Key-value lookup system in Latex macros fails if value has latex tokens - latex

I am trying to use a sort of key-value lookup system in a LaTeX document, but I am having problems when the value itself has LaTeX code within it. In my document, I have the following macro definitions:
\makeatletter
\def\NewLabel#1#2{\expandafter\xdef\csname LBL##1\endcsname{#2}}
\def\Ref#1{\#ifundefined{LBL##1}{???}{\csname LBL##1\endcsname}}
With these macros, I can do the following in my document:
\NewLabel{name1}{John Smith}
\begin{document}
\Ref{name1}
\end{document}
However, the following fails complaining about an extra }:
\usepackage{hyperref}
\NewLabel{name1}{\href{mailto:johnsmith#exmaple.com}{John Smith}}
\begin{document}
\Ref{name1}
\end{document}
I know the solution has to have something to do with \protect or \relax or something like that, but I can't figure out what it is.
Does anyone know the solution to this problem?

I found two solutions to my problem, one using \noexpand and the other changing \xdef to \gdef, though I must admit that I do not fully understand the consequences of my solutions (particularly the use of \gdef). The more narrow solution using \noexpand is:
\documentclass{article}
\makeatletter
\def\NewLabel#1#2{\expandafter\xdef\csname LBL##1\endcsname{#2}}
\def\Ref#1{\#ifundefined{LBL##1}{???}{\csname LBL##1\endcsname}}
\usepackage{hyperref}
\NewLabel{name1}{\noexpand\href{mailto:johnsmith#exmaple.com}{John Smith}}
\begin{document}
\Ref{name1}
\end{document}
The wider solution using \gdef is:
\documentclass{article}
\makeatletter
\def\NewLabel#1#2{\expandafter\gdef\csname LBL##1\endcsname{#2}}
\def\Ref#1{\#ifundefined{LBL##1}{???}{\csname LBL##1\endcsname}}
\usepackage{hyperref}
\NewLabel{name1}{\href{mailto:johnsmith#exmaple.com}{John Smith}}
\begin{document}
\Ref{name1}
\end{document}
Though I see why using \gdef instead of \xdef will avoid expanding the argument, which was previously causing the problem, I am not sure what negative consequences this could have. Is there a context where the change from \xdef to \gdef could cause problems?

Related

LaTeX change name of \bibliography{} heading (or remove entirely)

I have seen this question asked a few times on here but none of the answers have gotten me exactly what I want. I am using Overleaf to write a LaTeX document and when I make a bibliography (using the natbib library) it automatically makes a heading named "References". Can I remove this heading somehow? I've tried a couple other options but they haven't worked. I've attached a minimal working example below:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{natbib}
\usepackage{hyperref}
\begin{document}
Here is a citation \citep{Source}.
\bibliographystyle{apalike}
\bibliography{refs}
\end{document}
Not sure what didn't work with the existing responses - perhaps you could point to them and indicate what you didn't like about the results? Does replacing the \bibliography{refs} with the following help? (BTW, as this is not an Overleaf question, it's just LaTeX, perhaps remove the overleaf tag and reference.)
\begingroup
\renewcommand
\refname{}
\bibliography{refs}
\endgroup

LaTeX elsarticle style frontmatter

I am experiencing a slight annoyance with LaTeX and Elsevier's elsarticle style.
I cannot get rid of the superscript a in front of the affiliation.
It is needed for multiple authors, so affiliations can be associated with different authors. It is unnecessary in case of a single author.
I could not find a way to remove it.
Is there a way to accomplish this?
This is my LaTeX code.
\documentclass[preprint,12pt,authoryear,nopreprintline]{elsarticle}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\begin{document}
\SweaveOpts{concordance=TRUE}
\begin{frontmatter}
\title{Recursive Combinatorial Functions, Matrices,\\ and a Diagram Notation}
\author{Erich Neuwirth}%
\ead{erich.neuwirth#univie.ac.at}%
\affiliation{organisation={University of Vienna, Faculty of Computer Science}}%
\date{\today}
\end{frontmatter}
Some text
\end{document}

Treating a quote block similarly to a figure in LaTeX?

Been trying to figure out how to achieve this but can only find ways to do it within the figure environment using wrapfig. I'm using the epigraph package to generate the quote style (as in the screenshot). The current behavior is in this screenshot and generated by the code
\subsection{Methods}
\lipsum[1-2]
\epigraph{Models, of course, are never true, but fortunately it is only necessary that they be useful.}{\textcite{Box1979-em}}
However, the below image is how I wish it to look, but unsure how to achieve this?
Without a MWE, I had to make some adjustments; but this appears to work.
\documentclass[11pt,letterpaper]{article}
\usepackage{lipsum}
\usepackage{epigraph}
\usepackage{biblatex}
\usepackage{wrapfig}
\begin{document}
\setcounter{section}{2}
\subsection{Methods}
\begin{wrapfigure}{r}{0pt}%
\begin{minipage}{0.43\linewidth} % manual adjustment required
\epigraph{Models, of course, are never true, but fortunately it is only necessary that they be useful.}{Box (1979)}
\end{minipage}
\end{wrapfigure}
\lipsum[1-2]
\end{document}

Making a small modification to a LaTeX environment

I've been using \begin{figure} ... \end{figure} throughout my LaTeX document, but the default styling is ugly; namely, the figures are all left-aligned. Is there a way to redefine the "figure" environment so it automatically inserts some centering commands such as like this?:
\begin{figure} \begin{center}
\end{center} \end{figure}
Sure, I could use \newenvironment to define a "cfigure" environment, but that's undesirable. I don't want to go through and change all my "figures" to "cfigures" (and then later realise I wanted all the figures to be right-aligned and have to rename them all to "rfigures").
I could use \renewenvironment, but then I'd have to dig through the LaTeX source to find what the "figure" environment was originally defined as copy/paste it in.
I almost found what I wanted at this blog post, but the example there was for a command, not an environment.
\let\oldfigure\figure
\def\figure{\oldfigure\centering}
Another solution which works with the optional arguments.
Fixed.
\let\oldfigure\figure
\let\oldendfigure\endfigure
\def\figure{\begingroup \oldfigure}
\def\endfigure{\centering \oldendfigure \endgroup}
Fixed 2. It does work well with any options and any rules and \par inside.
\makeatletter
\let\oldfigure\figure
\def\figure{\#ifnextchar[\figure#i \figure#ii}
\def\figure#i[#1]{\oldfigure[#1]\centering}
\def\figure#ii{\oldfigure\centering}
\makeatother
As noted in another answer, you can't do the old trick of prepending commands to the end of the \figure macro because that will mess up the optional argument processing.
If an environment doesn't have arguments then it will work fine, but otherwise there's no straightforward way of doing this.
For your problem with the figures, try loading the floatrow package:
\usepackage{floatrow}
If will centre the content of your figures automatically.
Update: If you don't want to load a package, here's some code that will also do it. Note that it's specific to the figure environment, but the basic theme is: copy the original definition, parsing arguments the same way, then add whatever code you need at the end.
\makeatletter
\renewenvironment{figure}[1][\fps#figure]{
\edef\#tempa{\noexpand\#float{figure}[#1]}
\#tempa\centering
}{
\end#float
}
\makeatother
The \edef is required to fully expand \fps#figure before it's passed to the \#float macro.
How about:
\newenvironment{centeredfigure}{\begin{figure}\begin{center}}{\end{center}\end{figure}}
Note: untested.

Double-Spacing an ACM Proceedings Article in LaTeX

I am using the acm LaTeX template and I have trouble making my paper double spaced.
My LaTeX document looks like the following:
\documentclass{acm_proc_article-sp}
\usepackage{setspace}
\doublespacing
\begin{document}
...
\end{document}
When I compile the above document using pdflatex, I get the following error message on the line that I use the command \doublespacing:
Missing number, treated as zero \doublespacing
\linespread{2} should work. Doesn't need any packages, as far as I can tell, and you could change it to 1.9-spacing or 2.1-spacing, if you felt like it...
It looks like the acm_proc_article-sp class does some funky things that confuses the setspace package. In the preamble of your .tex document, add the following lines:
% Redefines \#ptsize to make setspace happy
\makeatletter
\renewcommand{\#ptsize}{0}
\makeatother
% Double-spaces the entire document
\usepackage{setspace}
\doublespacing
I have no idea why the acm_proc_article-sp class redefines \#ptsize to be empty.
I believe you want to use \usepackage{doublespace} to double-space your document. To put in a block of singlespacing, surround it with \begin{singlespace} and \end{singlespace}.
Ref: http://web.mit.edu/olh/Latex/ess-latex.html
The acm_proc_article-sp class seems to redefine \#ptsize to be empty for some reason. I don't know what \#ptsize is used for, so I don't want to mess with it. Using the \show command (see link text for more on this fantastic command), I see that \doublespacing is unpacked into
\setstretch{1.667} \ifcase \#ptsize \relax \setstretch{1.667} \or
\setstretch{1.618} \or \setstretch{1.655}\fi
In other words, \doublespacing is essentially equivalent to \setstretch{1.667}, with slightly different stretch factors if \#ptsize happens to be 1 or 2. So I think the most unobtrusive solution to your problem is replace \doublespacing by \setstretch{1.667}.
\documentclass{acm_proc_article-sp}
\usepackage{setspace}
\setstretch{1.667}
\begin{document}
...
\end{document}
I just tried out this command (in my preamble) for double spacing and it worked fine:
\usepackage{setspace}
\setstretch{2}
Alternative to the accepted solution: you can use \usepackage{setspace} if you include the line \doublespacing within your actual document (I have it right after the \maketitle command), instead of in your preamble. So something like:
\usepackage{fullpage}
\usepackage{setspace}
\begin{document}
\maketitle
\doublespacing
...
\end{document}
I'm not sure why this is different, but I think including it in the actual document automagically fills in the params you're missing.
Though this is an old thread, for those who stumble uppon:
What you have is actually correct and works. The problem is with the template that is being used. Probably it is overriding some command (like the \doublespacing).
This compiles and works fine for me:
\documentclass{article}
\usepackage{setspace}
\doublespacing
\begin{document}
testing
\end{document}
Don't do it. You need to follow the ACM conference proceedings article submission instructions, which don't want you to double-space the article anyway.

Resources