매크로 확장이 이상하게 충돌합니다.

매크로 확장이 이상하게 충돌합니다.

여기 내 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}

여기에 이미지 설명을 입력하세요

관련 정보