\newkeycommand に 2 つの \IfEq があると失敗します

\newkeycommand に 2 つの \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}

関連情報