![Dois \IfEq em \newkeycommand causam falha](https://rvso.com/image/348643/Dois%20%5CIfEq%20em%20%5Cnewkeycommand%20causam%20falha.png)
Considere este arquivo .tex
\documentclass[10pt]{article}
\usepackage{MWE}
\MWESettings[X=8]{article}
\begin{document}
The answer is \name
\end{document}
Onde 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
}
Se eu pdflatex o arquivo .tex, recebo este erro:
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.4 \begin{d
ocument}
O que estou fazendo de errado?
Responder1
Pelo que entendi seu código, você gostaria disso
\MWESettings[X=8]{article}
faz
\newcommand{\name}{article 8}
enquanto
\MWESettings[X=5]{news}
faz
\newcommand{\name}{news 5}
Você está esquecendo o “ramo falso”: deveria ser
\IfEq{<string-a>}{<string-b>}{<true>}{<false>}
Então isso 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}
Observe também que você está introduzindo muitos espaços indesejados em seu código.
Não está claro por que não usar diretamente #1
em vez do \IfEq
texto, mas acho que este é um exemplo simplificado.
Não que eu recomende keycommand
. Uma implementação diferente com 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}