![Dos \IfEq en \newkeycommand causan falla](https://rvso.com/image/348643/Dos%20%5CIfEq%20en%20%5Cnewkeycommand%20causan%20falla.png)
Considere este archivo .tex
\documentclass[10pt]{article}
\usepackage{MWE}
\MWESettings[X=8]{article}
\begin{document}
The answer is \name
\end{document}
Donde está 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
}
Si hago pdflatex el archivo .tex, aparece este error:
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.4 \begin{d
ocument}
¿Qué estoy haciendo mal?
Respuesta1
Hasta donde entiendo tu código, te gustaría eso
\MWESettings[X=8]{article}
hace
\newcommand{\name}{article 8}
mientras
\MWESettings[X=5]{news}
hace
\newcommand{\name}{news 5}
Te estás olvidando de la “rama falsa”: debería ser
\IfEq{<string-a>}{<string-b>}{<true>}{<false>}
Entonces esto funciona.
\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}
Tenga en cuenta también que está introduciendo muchos espacios no deseados en su código.
No está claro por qué no usar directamente #1
en lugar del \IfEq
texto, pero supongo que este es un ejemplo simplificado.
No es que lo recomiendo keycommand
. Una implementación diferente con 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}