使用 \soulregister 注册自定义命令后,Soul“\xxx 的参数有一个额外的 }”

使用 \soulregister 注册自定义命令后,Soul“\xxx 的参数有一个额外的 }”

我遇到了几个类似的问题和答案,例如https://tex.stackexchange.com/a/20055https://tex.stackexchange.com/a/46871,但即使注册了我的自定义命令,我仍然会收到类似的错误。不使用该\soulregister命令会产生完全相同的错误。

\newcommand{\code}[1]{\sethlcolor{gray}\hl{\texttt{#1}}}
\soulregister{\code}{1}

这是我收到的错误:

! Argument of \@textcolor has an extra }.
<inserted text> 
                \par 
l.276 \section{\code{virusscan}}

有问题的乳胶代码如下所示。我在回忆录文档中使用它。

\section{\code{virusscan}}

编辑:

定义命令如下:

\sethlcolor{gray}
\newcommand{\code}[1]{\hl{\texttt{#1}}}
\soulregister{\code}{1}

产量

! Argument of \let has an extra }.
<inserted text> 
                \par 
l.277 \section{\code{virusscan}}

\texttt{\hl{#1}}没有改变任何东西。


编辑2:

以下是最小的非工作示例:

\documentclass{memoir}

\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{soul}

\newcommand{\code}[1]{\sethlcolor{gray}\hl{\texttt{#1}}}
\soulregister{\code}{1}

\begin{document}

\section{\code{test}}

\end{document}

答案1

您还需要使命令变得健壮,因为您在移动参数中使用它。

\documentclass{memoir}

\usepackage{xcolor}
\usepackage{soul}

\DeclareRobustCommand{\code}[1]{{\sethlcolor{gray}\hl{\texttt{#1}}}}
\soulregister{\code}{1}

\begin{document}

\section{\code{test} and \hl{xyz}}

\end{document}

在此处输入图片描述

问题有所hyperref不同并且与书签有关。

\documentclass{memoir}

\usepackage{xcolor}
\usepackage{soul}
\usepackage{hyperref}

\DeclareRobustCommand{\code}[1]{{\sethlcolor{gray}\hl{\texttt{#1}}}}
\soulregister{\code}{1}

\begin{document}

\section{\texorpdfstring{\code{test}{test}}

\end{document}

或者,使用\pdfstringdefDisableCommands

\documentclass{memoir}

\usepackage{xcolor}
\usepackage{soul}
\usepackage{hyperref}

\DeclareRobustCommand{\code}[1]{{\sethlcolor{gray}\hl{\texttt{#1}}}}
\soulregister{\code}{1}
\makeatletter
\pdfstringdefDisableCommands{\let\code\@firstofone}
\makeatother

\begin{document}

\section{\code{test}}

\end{document}

答案2

\soulregister使用您的 MWE 时,无论是使用还是不使用, 我都会收到错误\soulregister

\code我仅在尝试在 -command的参数内使用 -command 时才会得到它\section

我还收到 hyperref-package 的警告:

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing `\code' on input line 12.

原因:

\section命令不仅会将一个漂亮的段落标题写入文本,还会将条目放入 .aux 文件中,以便写入目录的 .toc 文件,并将条目放入 .out 文件中,以便超链接包可以为章节标题创建书签。如果页面样式包含页面标题,它还会更改页面标题。

为了确保\code在写入 .toc 文件的辅助文件条目和 .toc 文件本身时 -command 不会展开,请根据 来定义它,\DeclareRobustCommand而不是根据 来定义它\newcommand。为了确保此命令不会滑入 hyperref-bookmarks(其中只应出现纯文本),请\texorpdfstring在 的定义内或将其放入sectioning-commands 或 captions 的参数中\code时使用 。\code

或者解决超链接-issue,正如 egreg 指出的那样,使用\pdfstringdefDisableCommands。(似乎他在我写答案的时候给出了他的答案 ;-)。)

\documentclass{memoir}

\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{soul}

\DeclareRobustCommand{\code}[1]{%
  \texorpdfstring{{\sethlcolor{gray}\hl{\texttt{#1}}}}%
                 {#1}%
}
\soulregister{\code}{1}

\begin{document}

%\tableofcontents

\section{\code{test}}

\end{document}

顺便一提: \code不仅在段落标题中提供灰色背景颜色,而且在目录条目和页面标题中也提供灰色背景颜色。

如果您不喜欢这样,但希望只在段落标题中使用颜色,那么一个粗略的解决方法是检查 的含义\protect。在某些情况下,用于防止特定标记扩展的 LaTeX 2e 保护机制会根据情况重新定义该标记,其中情况可能是“在页面上生成普通文本”,“为 .aux 文件生成一个条目,该文件将在 LaTeX 运行结束时进入 .toc 文件”,...

在下面的例子中,灰色背景颜色仅在段落标题中提供,而不是在页面标题和目录中提供:

\documentclass{memoir}

\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{soul}

\makeatletter
\newcommand\code{%
  \ifx\protect\@unexpandable@protect
    \expandafter\codeinmovingarg
  \else
    \expandafter\codeintext
  \fi
}%
\makeatother
\DeclareRobustCommand{\codeinmovingarg}[1]{%
  \texorpdfstring{\texttt}{}{#1}%
}%
\DeclareRobustCommand{\codeintext}[1]{%
  \texorpdfstring{{\sethlcolor{gray}\hl{\texttt{#1}}}}%
                 {#1}%
}%
\soulregister{\code}{1}

\begin{document}

\tableofcontents\cleardoublepage

\section{\code{test}}

\end{document}

再顺便说一下:

分段命令也会影响页标题。

页面标题默认为“大写”,回忆录。 在回忆录\uppercaseheads您可以通过/启用/禁用页面标题的大写\nouppercaseheads

如果启用了页面标题的大写,\section则出现在 -macro 参数中的代码(因此也会出现在页面标题中)也将在页面标题中大写。

我不喜欢将代码大写。如果您也不喜欢这样,您可以在\section调用\code-macro 的参数中将 -macro 的参数\NoCaseChange作为需要\protect大写的参数:

\documentclass{memoir}

\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{soul}

\DeclareRobustCommand{\code}[1]{%
    \texorpdfstring{{\sethlcolor{gray}\hl{\texttt{#1}}}}%
                   {#1}%
}%
\soulregister{\code}{1}

\begin{document}

\tableofcontents
\cleardoublepage

\section{\protect\NoCaseChange{\code{test}}}

\end{document}

答案3

我在这里添加答案而不是添加新问题,因为我有一个极其相似问题的代码略有不同,这里提供的答案给了我解决问题所需的提示,尽管方式不同。

最小(但愿不工作)示例:

\documentclass{article}
\usepackage{hyperref}
\usepackage{xcolor}
\usepackage{soul}

\begin{document}

\begin{table}
        \caption{
                Something {\sethlcolor{blue}\hl{highlighted}} and that's all.
        }
\end{table}

\end{document}

诊断

即使文档中没有表格列表,标题也可能出现在文件中aux,由于多余的而发生了一些不好的事情\@textcolor

解决方法

我可以把它变成一个命令,类似于其他答案中提出的内容。

在这种情况下,更简单的方法(可能是一种很好的做法)是为aux文件添加一个简短的标题,其中不包含突出显示命令,即使它没有显示在最终文档的任何地方(无论如何我可能会选择稍后添加表格列表):


\begin{table}
        \caption[A table with some highlight in the caption.]{
                Something {\sethlcolor{blue}\hl{highlighted}} and that's all.
        }
\end{table}

¹ 不幸的是,我无法在本地重现该问题,无论是使用这个最小示例还是使用另一个示例,因此我不确定它是否正确地突出了该问题。该问题出现在 CI 系统(基于 Debian)内不同版本的 TeXLive 中。该示例仍然适合说明该问题。

相关内容