宏比较失败

宏比较失败

我是 LaTex 新手,同事给了我一个问题。他们在 TeX 文件中定义了一种文档类型(例如“svvr”、“srs”等)。此外部定义对应于我的示例代码(MWE 2)中的第 4 行和第 5 行。

现在我应该在我的 TeX 文件中使用此外部定义来决定应进行哪些与文档相关的处理。为此,我编写了一个 MWE 来测试选择。

我的测试代码最初来自互联网,它将作业名称与给定的字符串进行比较(例如“{\detokenize{svvr}”)。我将我的 TeX 文件命名为“svvr.tex”,以便能够与 \jobname 进行比较。

我现在的问题是,当我与 \jobname (MWE 1) 进行比较时,选择可以正确进行。

\documentclass{article}

% Own TeX file:
\begin{document}
    \edef\docTypeExt{\jobname}
    % Specify which document type is to be handled: 'svvr', 'srs', ...
    \edef\docTypeSvvr{\detokenize{svvr}} 
    \edef\docTypeSrs{\detokenize{srs}}

    \textbf{\jobname} 
    \textbf{\docShortcut}
    \textbf{\docTypeSvvr}
    \textbf{\docTypeSrs}
    \newline
    
    \ifx\docTypeExt\docTypeSvvr
        true
    \else
        false: \\
        \jobname \\
        \docShortcut \\
        \docTypeSvvr
    \fi
\newline

    \ifx\docTypeExt\docTypeSrs
        true
    \else
        false: \\
        \jobname \\
        \docShortcut \\
        \docTypeSrs
    \fi

\end{document}

但如果我将其与同事的外部定义(MWE 2)进行比较,比较就会失败。

\documentclass{article}

% Given code, defined in included TeX file: 'svvr', 'srs', ...
\newcommand { \docpropPTdocShortcut } {svvr}    % Define document type here!
\gdef\docShortcut{\docpropPTdocShortcut}

% Own TeX file:
\begin{document}
    \edef\docTypeExt{\docShortcut}
    % Specify which document type is to be handled: 'svvr', 'srs', ...
    \edef\docTypeSvvr{\detokenize{svvr}} 
    \edef\docTypeSrs{\detokenize{srs}}

    \textbf{\jobname} 
    \textbf{\docShortcut}
    \textbf{\docTypeSvvr}
    \textbf{\docTypeSrs}
    \newline
    
    \ifx\docTypeExt\docTypeSvvr
        true
    \else
        false: \\
        \jobname \\
        \docShortcut \\
        \docTypeSvvr
    \fi
\newline

    \ifx\docTypeExt\docTypeSrs
        true
    \else
        false: \\
        \jobname \\
        \docShortcut \\
        \docTypeSrs
    \fi

\end{document}

必须如何构建选择才能使其与外部定义兼容?

我已经修改了定义,但迄今为止所有的尝试都失败了。

答案1

传递的每个字符标记都\jobname属于类别 12(其他)。例外:空格(ASCII 和 Unicode 中的代码点编号均为 32)属于类别 10(空格)。与 传递的字符标记相同\detokenize

但是,第二个示例中的替换文本中的字符标记\docpropPTdocShortcut不是由\jobname或提供的\detokenize,而是由于 TeX 读取和标记 .tex 输入文件中的内容而产生的。因此,应用了另一种类别代码机制,因此以这种方式产生的许多字符标记具有与 12(其他)不同的类别。例如,以这种方式产生的表示字母表字母的字符标记通常具有类别 11(字母)。

因此,控制序列标记\docTypeSvvr\docTypeSrs(其替换文本仅由来自\jobname或的字符标记组成\detokenize,因此是类别 12(其他)的字符标记或类别 10(空格)的空格)的含义与控制序列(\docTypeExt其中相同字符被标记为类别 11(字母))的含义不同。
因此,\ifx与第二个示例进行比较将路由到\else分支。

对于第二个例子,您可以\detokenize在定义时省略\docTypeSvvr/ ,或者在定义时\docTypeSrs应用\detokenize或。\@onelevel@sanitize\docTypeExt

\newline,这会导致 LaTeX 继续排版段落,但开始段落的另一行,后面跟着一个空行,这会导致 LaTeX 结束当前段落,这对我来说似乎有点多余:如果垂直线不为零,则\baselineskip从中获得垂直线,然后是\newline垂直线。如果垂直线为零,并且在某些地方您希望在段落之间跳过一些垂直线,请不要滥用此功能,而应使用或或或或或。\parskip\parskip\newline\vspace\addvspave\vskip\smallskip\medskip\bigskip

\newcommand { \docpropPTdocShortcut } {svvr}\docpropPTdocShortcut其中标记化之后前面是空格标记,这不是好的做法。如果不在 ExplSyntax 中,在标记化事物时空格会被忽略,请执行以下操作:
\newcommand{\docpropPTdocShortcut}{svvr}

通过下面的示例,可以采用 -route,因此您可以安全地将其替换文本由“如果您愿意”\detokenize的扩展组成的宏进行比较。\jobname

\documentclass{article}


% Given code, defined in included TeX file: 'svvr', 'srs', ...
%% (In case the TeX file in question does not produce text/a chapter of the document, 
%%  \input might be a better choice than \include ...)

\newcommand{\docpropPTdocShortcut}{svvr}% Define document type here!
\gdef\docShortcut{\docpropPTdocShortcut}

% Own TeX file:
\begin{document}
  \edef\docTypeExt{\docShortcut}%%
  \edef\docTypeExt{\detokenize\expandafter{\docTypeExt}}%%
  %\csname @onelevel@sanitize\endcsname\docTypeExt
  %-----------------------------------------------------------------  
  % Specify which document type is to be handled: 'svvr', 'srs', ...
  \edef\docTypeSvvr{\detokenize{svvr}}%%
  \edef\docTypeSrs{\detokenize{srs}}%%

  \noindent
  \verb|\jobname|: \texttt{\jobname}\\
  \verb|\docShortcut|: \texttt{\meaning\docShortcut}\\
  \verb|\docTypeExt|: \texttt{\meaning\docTypeExt}\\
  \verb|\docTypeSvvr|: \texttt{\meaning\docTypeSvvr}\\
  \verb|\docTypeSrs|: \texttt{\meaning\docTypeSrs}

  \vskip\baselineskip

  \noindent
  \ifx\docTypeExt\docTypeSvvr
      true
  \else
      false:\\
      \jobname\\
      \docShortcut\\
      \docTypeSvvr
  \fi

  \vskip\baselineskip

  \noindent
  \ifx\docTypeExt\docTypeSrs
      true
  \else
      false:\\
      \jobname\\
      \docShortcut\\
      \docTypeSrs
  \fi
\end{document}

相关内容