0

\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.

Jakob
  • 993
  • 2
    why on earth do you want to save the color there? – Ulrike Fischer Nov 08 '23 at 14:51
  • 2
    Maybe I'm old-fashioned, but I consider an insult to the reader having a link to the glossary for every appearance of some notation. – egreg Nov 08 '23 at 14:55
  • 1
    as before you could use \protect but you should not be making definitions in a caption. – David Carlisle Nov 08 '23 at 14:58
  • @UlrikeFischer I only want X to have the linkcolor, but not its index nor its power – Jakob Nov 08 '23 at 14:59
  • or replace newcommand by \DeclareRobustCommand – David Carlisle Nov 08 '23 at 14:59
  • Every caption will be written to the aux file for which it will be expanded. Use \protect in front of \colorlet or make your \Xsubsuper robust 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 the caption is placed. Either do it before the \caption or 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
  • @DavidCarlisle \protect inside 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
  • \protect was from the first version using \colorlet directly, 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
  • 1
    sorry but like @egreg I think you are overdoing. I simply loath documents where you jump around as soon as you touch something because everything has been turned into a link. Beside this, be aware that ifthenelse isn't the fastest command. – Ulrike Fischer Nov 08 '23 at 15:12
  • @DavidCarlisle I would prefer to do so, but unfortunately hyperlinks get boxed, so that \X_{sub}^{super} would result in wrong spacing as shown in the example. – Jakob Nov 08 '23 at 15:14
  • 1
  • @Jakob maybe you should instead ask about that. Possibly something like the \mathcolor addition to \textcolor which avoids that problem for color could be thought of. – David Carlisle Nov 08 '23 at 15:17
  • 1
    @Jakob $\rlap{\glslink{X}{\phantom{X}}}\mathcolor{linkcolor}{X}_1^3$ ? – David Carlisle Nov 08 '23 at 15:24
  • @DavidCarlisle This sounds interesting. egreg thought yesterday, that such as solution would probably not really be possible: https://chat.stackexchange.com/transcript/message/64681769#64681769 – Jakob Nov 08 '23 at 15:28
  • never trust @egreg :-) – David Carlisle Nov 08 '23 at 15:30
  • @DavidCarlisle with lualatex it should be possible to use attributes to create the links, that wouldn't affect the spacing at all, see e.g. https://github.com/zauguin/lua-links – Ulrike Fischer Nov 08 '23 at 17:07
  • @UlrikeFischer yes – David Carlisle Nov 08 '23 at 17:08
  • @DavidCarlisle $\mathrlap{\glslink{X}{\phantom{X}}}\mathcolor{linkcolor}{X}_1^3$ is even better with \mathrlap from the package mathtools compared 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

1 Answers1

2

If you really need such constructs you should hide them in a robust command, eg as defined by \NewDocumentCommand

\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} }

\NewDocumentCommand\Xsubsuper{mm}{\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}

David Carlisle
  • 757,742