4

I defined the following command:

\NewDocumentCommand \displayls {s m}{%
  \IfBooleanTF{#1}{\textls*[250]{#2}}{\textls[250]{#2}}%
}

I would like to refactor the definition to remove the duplication.

I can make a trivial version:

\NewDocumentCommand \displayls {s m}{%
  \csname textls\endcsname[250]{#2}%
}

If I then try to process the star argument, I get into trouble. I tried:

\NewDocumentCommand \displayls {s m}{%
  \csname textls\IfBooleanTF{#1}{*}{}\endcsname[250]{#2}%
}

But this does not work for the *-form; it produces:

enter image description here

Here is the test document:

\documentclass{article}

\usepackage{microtype,xparse}
\NewDocumentCommand \displayls {s m}{%
  % (One of the definitions above)
}

\begin{document}
\displayls{foo}
\displayls*{foo}
\end{document}

I tried adding \expandafter before \csname, but that didn't make any difference.

1

1 Answer 1

3

Just duplicate \textls; or use \exp_last_unbraced:Nf (but I wouldn't bother to).

\documentclass{article}

\usepackage{microtype,xparse}

\NewDocumentCommand{\displayls}{s m}{%
  \IfBooleanTF{#1}{\textls*}{\textls}[250]{#2}%
}

\ExplSyntaxOn
\NewDocumentCommand\displaylsvar{s m}
 {
  \exp_last_unbraced:Nf \textls \IfBooleanT{#1}{*} [250] {#2}
 }
\ExplSyntaxOff

\begin{document}

\displayls{foo}
\displayls*{foo}

\displaylsvar{foo}
\displaylsvar*{foo}

\end{document}

enter image description here

Just for information about your attempt. If you call \textls* it's two tokens, namely \textls and *; to the contrary, \csname textls*\endcsname constructs a single token (undefined, so treated like \relax).

Also, here's the definition of \textls by microtype:

\DeclareRobustCommand\textls{%
  \@ifstar{\let\MT@ls@adjust@\MT@ls@adjust@empty\MT@textls}%
          {\let\MT@ls@adjust@\MT@ls@adjust@relax\MT@textls}%
}

I see no much point in avoiding the duplication in the suggested simple version.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .