标题内的 \colorlet{saved}{.} 产生错误

标题内的 \colorlet{saved}{.} 产生错误

\colorlet{saved}{.}里面caption会产生错误。我不明白为什么?我该如何解决这个问题?有没有更好的替代方案\colorlet{saved}{.}

梅威瑟:

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

现实例子:

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

据我所知,它生成了正确的 pdf,但它给出了 2 条错误消息:

\Hy@tempa 的参数有一个额外的}。

<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 type `2' and it will go away.

失控的争论?

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

答案1

如果你真的需要这样的构造,你应该将它们隐藏在一个强大的命令中,例如定义\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}

相关内容