巨集比較失敗

巨集比較失敗

我是 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不是由 TeX 傳遞的\jobname\detokenize也不是由於 TeX 從 .tex-input 檔案中讀取和標記內容而產生的。由此應用另一個類別代碼制度,使得以這種方式形成的許多字元標記具有與12(其他)不同的類別。例如,以這種方式產生的表示字母表中的字母的字元標記通常具有類別11(字母)。

因此,控制序列標記\docTypeSvvr\docTypeSrs,其替換文字僅由來自\jobname或的字元標記組成\detokenize,因此是類別 12(其他)的字元標記或類別 10(空格)的空格,其含義與控制項的含義不同\docTypeExt相同字元被標記為類別11(字母)的序列。
因此\ifx-comparison 與第二個範例路由到\else-branch。

在第二個範例中,您可以\detokenize在定義\docTypeSvvr/時省略或在定義時\docTypeSrs套用\detokenizeor 。\@onelevel@sanitize\docTypeExt

\newline,這會導致 LaTeX 繼續排版段落,但開始段落的另一行,後面有一個空行,這會導致 LaTeX 結束當前段落,對我來說似乎有點多餘:您可以\baselineskip\newline和 垂直\parskip如果不為零。如果垂直\parskip為零,並且在某些地方您希望在段落之間進行一些垂直跳躍,請不要濫用\newline此功能,而是使用\vspaceor \addvspaveor \vskipor \smallskipor or \medskipor \bigskip

\newcommand { \docpropPTdocShortcut } {svvr}\docpropPTdocShortcut在標記化之後前面有一個空格標記,這不是一個好的做法。如果不在 ExplSyntax 中,在標記事物時空格將被忽略,請執行:
\newcommand{\docpropPTdocShortcut}{svvr}

在下面的範例中,\detokenize採用了 -route,以便您可以安全地與巨集進行比較,巨集的替換文字是由 if 您喜歡的擴充功能組成的\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}

相關內容