\colorlet{saved}{.} inside caption produces an error. I don't understand why? How can I fix this? Are there better alternatives to \colorlet{saved}{.}?
MWE:
\documentclass{article}
\usepackage{amsmath,xcolor}
\usepackage{graphicx}
\begin{document}
\begin{figure}[h]
\caption{Example of a parametric plot ($\colorlet{saved}{.}\sin (x), \cos(x), x$)}
\centering
\includegraphics[width=0.5\textwidth]{example-image}
\end{figure}
\end{document}
realistic example:
\documentclass{article}
\usepackage{xifthen}
\usepackage{amsmath,xcolor}
\usepackage[colorlinks]{hyperref}
\usepackage{glossaries}
\usepackage{graphicx}
\colorlet{linkcolor}{red!70}
\hypersetup{linkcolor=linkcolor}
\makeglossaries
\newglossaryentry{X}
{
name={\ensuremath{X}},
description={Some quantity that changes over time can have index (corresponding to the time) and superscript}
}
\newcommand{\Xsubsuper}[2]{\colorlet{saved}{.}\ensuremath{\glslink{X}{X\ifthenelse{\isempty{#1}}{}{_{\mathcolor{saved}{#1}}}\ifthenelse{\isempty{#2}}{}{^{\mathcolor{saved}{#2}}}}}%
}
\newglossaryentry{t}
{
name={\ensuremath{t}},
description={time can have index and superscript}
}
\newcommand{\tsubsuper}[2]{\colorlet{saved}{.}\ensuremath{\glslink{t}{t\ifthenelse{\isempty{#1}}{}{_{\mathcolor{saved}{#1}}}\ifthenelse{\isempty{#2}{}}{}{^{\mathcolor{saved}{#2}}}}}%
}
\begin{document}
\begin{figure}[h]
\caption{\color{blue} Example of a parametric plot ($\sin (\Xsubsuper{3}{5}), \cos(x), x$)}
\centering
\includegraphics[width=0.5\textwidth]{example-image}
\end{figure}
\noindent$X_1^3$\
$\Xsubsuper{}{}_1^3$ wrong spacing\
$\Xsubsuper{1}{3}$ correct spacing\
$\Xsubsuper{}{3}$\
{\color{green}
$\tsubsuper{1}{3}$\
$\tsubsuper{}{3}$\
$\Xsubsuper{\tsubsuper{1}{3}}{3}$}
\printglossary[title=Notation,toctitle=Notation]
\end{document}
It produces the correct pdf as far as I can see, but it gives 2 error messages:
Argument of \Hy@tempa has an extra }.
<inserted text>
\par
l.33 ...t ($\sin (\Xsubsuper{3}{5}), \cos(x), x$)}
I've run across a }' that doesn't seem to match anything. For example,\def\a#1{...}' and \a}' would produce this error. If you simply proceed now, the\par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your }' was spurious, just type2' and it will go away.
and
Runaway argument?
\@captype {\XC@colorlet }\def \reserved@b {\XC@colorlet [{}]}\futurelet \ETC.
! Paragraph ended before \Hy@tempa was complete.
<to be read again>
\par
l.33 ...t ($\sin (\Xsubsuper{3}{5}), \cos(x), x$)}
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.
\protectbut you should not be making definitions in a caption. – David Carlisle Nov 08 '23 at 14:58newcommandby\DeclareRobustCommand– David Carlisle Nov 08 '23 at 14:59\protectin front of\colorletor make your\Xsubsuperrobust but that will save the color once in the table of figures and once with the original\caption. You probably want to save the color only once where thecaptionis placed. Either do it before the\captionor in a way that it is not written to the table of figures. (Some classes allow\caption[entry for table of figures]{entry for caption}.) — What are you even trying to do? Maybe there's a much better approach. – Qrrbrbirlbel Nov 08 '23 at 15:00\protectinside the \newcommand does not help. Directly in front oft \Xsupsuper it works well :) I don't want to define X in the caption, but I want every occurrence of X to be linked to the notation glossary. It's nice for reading a paper with heavy notation (especially since some pdf viewers support a little preview of hyperlinks at mouse-over :) – Jakob Nov 08 '23 at 15:09\protectwas from the first version using\colorletdirectly, see answer to make your defined command robust. But why do this at all why not simply make the base of your construct the link so the subscripts naturally do not pick up the link colour? – David Carlisle Nov 08 '23 at 15:11\X_{sub}^{super}would result in wrong spacing as shown in the example. – Jakob Nov 08 '23 at 15:14\mathcoloraddition to\textcolorwhich avoids that problem for color could be thought of. – David Carlisle Nov 08 '23 at 15:17$\rlap{\glslink{X}{\phantom{X}}}\mathcolor{linkcolor}{X}_1^3$? – David Carlisle Nov 08 '23 at 15:24$\mathrlap{\glslink{X}{\phantom{X}}}\mathcolor{linkcolor}{X}_1^3$is even better with\mathrlapfrom the packagemathtoolscompared to\rlap. Because it can also be used inside supscripts and superscripts: https://tex.stackexchange.com/questions/701482/rlap-in-subscript-leads-to-larger-subscript. – Jakob Nov 16 '23 at 14:19