1

Consider this .tex file

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

Where MWE.sty is

\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
 }

If I pdflatex the .tex file I get this error:

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

l.4 \begin{d
            ocument}

What am I doing wrong?

cgnieder
  • 66,645
Don Kreher
  • 412
  • 4
  • 12
  • So you'd like that \MWEsettings[X=8]{article} essentially does \newcommand{\name}{article 8}? – egreg Jan 30 '17 at 21:29
  • No not really. This is just a simplified example. I actually want it to set up say different page headings depending on the type of manuscript or perhaps other style changes. Maybe I have over simplified. – Don Kreher Jan 30 '17 at 21:32
  • @DonKreher: You can't use your \MWESettings command before \begin{document} this way –  Jan 30 '17 at 21:38

1 Answers1

1

As far as I understand your code, you'd like that

\MWESettings[X=8]{article}

does

\newcommand{\name}{article 8}

while

\MWESettings[X=5]{news}

does

\newcommand{\name}{news 5}

You're forgetting the “false branch”: it should be

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

So this works.

\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}

Note also that you're introducing many unwanted spaces in your code.

It's not clear why not directly using #1 instead of the \IfEq text, but I guess this is a simplified example.

Not that I recommend keycommand. A different implementation with 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}
egreg
  • 1,121,712