宏觀擴張奇怪地崩潰

宏觀擴張奇怪地崩潰

這是我的 mwe.tex

\documentclass[12pt]{report}
\usepackage{xifthen}
\usepackage{xstring}

\def\test{\textnormal{test}}

\newcounter{wordCount}
\setcounter{wordCount}{0}
\newcounter{AllWord}
\setcounter{AllWord}{0}

\NewDocumentCommand{\exy}{ m m m}{%
    \StrCount{#2}{,}[\comma]
    \StrCount{#2}{?}[\qmark]
    \ifthenelse{\qmark > 0}{%
        \addtocounter{wordCount}{\comma + \qmark}
        \addtocounter{AllWord}{\comma + \qmark}
    }{%
        \addtocounter{wordCount}{\comma + \qmark + 1}
        \addtocounter{AllWord}{\comma + \qmark + 1}
    }
    \textbf{#2} (#3), ``#1''\\%
}

\begin{document}
    \test\par
    \exy{one \test}{two \test s}{three \test s}
\end{document}

如果您註解掉該行,\exy它將按預期工作。否則錯誤開始於

[{
    "resource": "/c:/Users/hsmye/LaTeX/Gaelic/vocab/mwe.tex",
    "owner": "LaTeX",
    "severity": 8,
    "message": "Undefined control sequence.\n\\reserved@a ->\\@nil",
    "source": "LaTeX",
    "startLineNumber": 27,
    "startColumn": 1,
    "endLineNumber": 27,
    "endColumn": 65536
}]

這超出了我的宏福範圍。有人可以幫忙嗎?需要注意的是,這並不是最初的問題。為了在不需要購買字體的情況下生成 MWE,我不斷取出直到沒有錯誤,然後添加回去以獲得這個。原來的問題是:

\newfontfamily{\EtGoudy}{P22 Goudy Ampersands}
\DeclareTextFontCommand{\GoudyEt}{\EtGoudy}
\newfontfamily{\cVirgi}{P22 Virginian}
\DeclareTextFontCommand{\Virgic}{\cVirgi}
\def\dispEt{\GoudyEt{c}\kern -3pt{\vspace{-0.01ex}\huge\Virgic{c}}.}
\def\Etc{\textnormal{\dispEt}}

替換\test\Etc具有類似結果。

答案1

請注意,您應該始終以多行形式顯示 TeX 錯誤,因為換行符號對於理解訊息至關重要,它們標記哪個命令未定義

! Undefined control sequence.
\reserved@a ->\@nil 
                    
l.29 ...exy{one \test}{two \test s}{three \test s}
                                                  
?

您使用了 TeX 原語定義,導致了一個脆弱的命令在擴充上下文中中斷。如果您使用,\NewDocumentCommand您可以\protected在此類結構中獲得命令安全。

這運行沒有錯誤


\documentclass[12pt]{report}
\usepackage{xifthen}
\usepackage{xstring}

\NewDocumentCommand\test{}{\textnormal{test}}

\newcounter{wordCount}
\setcounter{wordCount}{0}
\newcounter{AllWord}
\setcounter{AllWord}{0}

\NewDocumentCommand{\exy}{ m m m}{%
    \StrCount{#2}{,}[\comma]%%%%%%%%%%%%%%
    \StrCount{#2}{?}[\qmark]%%%%%%%%%%%%%%
    \ifthenelse{\qmark > 0}{%
        \addtocounter{wordCount}{\comma + \qmark}%%%%%%%%%%%%%%
        \addtocounter{AllWord}{\comma + \qmark}%%%%%%%%%%%%%%
    }{%
        \addtocounter{wordCount}{\comma + \qmark + 1}%%%%%%%%%%%%%%
        \addtocounter{AllWord}{\comma + \qmark + 1}%%%%%%%%%%%%%%
    }%%%%%%%%%%%%%%
    \textbf{#2} (#3), ``#1''\\%
}

\begin{document}
    \test\par
    \exy{one \test}{two \test s}{three \test s}
\end{document}

但產生

Underfull \hbox (badness 10000) in paragraph at lines 29--30

由於位置錯誤\\,絕不應該在段落末尾使用它。 (使用的話會更好\par

答案2

我不會用xstring。程式碼中的主要問題是該命令\Etc(非常)脆弱。

我的建議是expl3透過分割逗號和問號的文字並計算項目數(並減一)來計算逗號和問號的數量。

該參數被完全擴展(但健壯的命令沒有)和“純化”,因此諸如此類的命令\textnormal消失了。

\documentclass[12pt]{report}

\newcommand\test{\textnormal{test}}
\NewDocumentCommand\dispEt{}{\GoudyEt{c}\kern -3pt{\huge\Virgic{c}}.}
\NewDocumentCommand\Etc{}{\textnormal{\dispEt}}
\newcommand\GoudyEt{}% just for testing
\newcommand{\Virgic}{}% just for testing

\newcounter{wordCount}
\newcounter{AllWord}

\ExplSyntaxOn

\NewDocumentCommand{\exy}{m m m}
 {
  \hsmyers_xyz_exy:nnen { #1 } { #2 } { \text_purify:n { \text_expand:n { #2 } } } { #3 }
 }

\int_new:N \l_hsmyers_xyz_comma_int
\int_new:N \l_hsmyers_xyz_qmark_int

\cs_new_protected:Nn \hsmyers_xyz_exy:nnnn
 {
  \hsmyers_xyz_count:nnN { , } { #3 } \l_hsmyers_xyz_comma_int
  \hsmyers_xyz_count:nnN { ? } { #3 } \l_hsmyers_xyz_qmark_int
  \int_compare:nTF { \l_hsmyers_xyz_qmark_int > 0 }
   {
    \addtocounter{wordCount}
     {
      \int_eval:n { \l_hsmyers_xyz_comma_int + \l_hsmyers_xyz_qmark_int }
     }
    \addtocounter{AllWord}
     {
      \int_eval:n { \l_hsmyers_xyz_comma_int + \l_hsmyers_xyz_qmark_int }
     }
    }{%
    \addtocounter{wordCount}
     {
      \int_eval:n { \l_hsmyers_xyz_comma_int + \l_hsmyers_xyz_qmark_int + 1}
     }
    \addtocounter{AllWord}
     {
      \int_eval:n { \l_hsmyers_xyz_comma_int + \l_hsmyers_xyz_qmark_int + 1}
     }
   }
  \textbf{#2}~#4,~``#1''
 }
\cs_generate_variant:Nn \hsmyers_xyz_exy:nnnn { nne }

\cs_new_protected:Nn \hsmyers_xyz_count:nnN
 {
  \seq_set_split:Nnn \l_tmpa_seq { #1 } { #2 }
  \int_set:Nn #3 { \seq_count:N \l_tmpa_seq - 1 }
 }

\ExplSyntaxOff

\begin{document}

\test\par

\exy{one \Etc}{two \test \Etc{} s}{three \Etc{} s}

wordCount=\the\value{wordCount}

AllWord=\the\value{AllWord}

\exy{one \test}{two, three?}{three \test s}

wordCount=\the\value{wordCount}

AllWord=\the\value{AllWord}

\end{document}

在此輸入影像描述

相關內容