\newkeycommand의 두 \IfEq로 인해 오류 발생

\newkeycommand의 두 \IfEq로 인해 오류 발생

이 .tex 파일을 고려해보세요

\documentclass[10pt]{article}
\usepackage{MWE}
\MWESettings[X=8]{article}
\begin{document}
The answer is \name
\end{document}

MWE.sty가 있는 곳

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{MWE}
\RequirePackage{xstring}
\RequirePackage{keycommand}
\newkeycommand{\MWESettings}[X=1][1]
 {
  \IfEq{#1}{article} { \newcommand{\name}{article \commandkey{X}} } <-- Line A
  \IfEq{#1}{news}    { \newcommand{\name}{news    \commandKey{X}} } <-- Line B
 }

.tex 파일을 pdflatex로 처리하면 다음 오류가 발생합니다.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.4 \begin{d
            ocument}

내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변1

내가 당신의 코드를 이해하는 한 당신은 그것을 원할 것입니다

\MWESettings[X=8]{article}

하다

\newcommand{\name}{article 8}

~하는 동안

\MWESettings[X=5]{news}

하다

\newcommand{\name}{news 5}

당신은 "거짓 가지"를 잊어버렸습니다.

\IfEq{<string-a>}{<string-b>}{<true>}{<false>}

그래서 이것은 작동합니다.

\documentclass[10pt]{article}
\usepackage{xstring}
\usepackage{keycommand}
\newkeycommand{\MWESettings}[X=1][1]
 {%
  \IfEq{#1}{article}{\newcommand{\name}{article \commandkey{X}}}{}% <-- Line A
  \IfEq{#1}{news}   {\newcommand{\name}{news    \commandkey{X}}}{}% <-- Line B
 }

\MWESettings[X=8]{article}

\begin{document}
The answer is \name
\end{document}

또한 코드에 원하지 않는 공백이 많이 발생한다는 점에 유의하세요.

#1텍스트 대신 직접 사용하지 않는 이유는 확실하지 않지만 \IfEq이는 단순화된 예라고 생각합니다.

내가 추천하는 것은 아닙니다 keycommand. 다음을 사용한 다른 구현 xparse:

\documentclass[10pt]{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\MWESettings}{O{X=1}m}
 {
  \keys_set:nn { kreher/mwe } { #1 }
  \cs_new:Npx \name
   {
    \str_case:nn { #2 }
     {
      {article}{article}
      {news}{news}
     }~\l_kreher_mwe_x_tl
   }
 }
\keys_define:nn { kreher/mwe }
 {
  X .tl_set:N = \l_kreher_mwe_x_tl,
 }
\ExplSyntaxOff

\MWESettings[X=8]{article}

\begin{document}
The answer is \name
\end{document}

관련 정보