2

I'm looking for some good advice on switching from (La)TeX to LaTeX3

Here is the first questions corresponding to the toy code below.

  1. What is a good way to manage an optional argument with LaTeX3?

  2. What is a good way to define star versions with LaTeX3?

\documentclass[12pt,a4paper]{article}

\usepackage{ifthen}

\makeatletter

\newcommand\test{\@ifstar{\@test@star}{\@test@no@star}}

\newcommand\@test@no@star[1]{%
    I am a %
    \ifthenelse{\equal{#1}{upper}}%
               {TSAR}%
               {tsar}%
    .
}

\newcommand\@test@star{%
    I am a star.%
}

\makeatother


\begin{document}

\test{}

\test{upper}

\test*{}

\end{document}

3 Answers 3

3

Defining a command with a *-variant is very simple:

\NewDocumentCommand{\test}{sm}{%
  \IfBooleanTF{#1}
    {% * is present
     I am a star.%
    }
    {% no *
     \testupper{#2}%
    }%
}

How to define \testupper to check whether the argument is upper? There is no “user level version”, and you need to go to the expl3 level; defining your own comparator is recommended:

\ExplSyntaxOn
\NewExpandableDocumentCommand{\comparestringTF}{mmmm}
 {% #1 = input string, #2 = fixed string, #3 = true text, #4 = false text
  \str_if_eq:nnTF { #1 } { #2 } { #3 } { #4 }
 }
\ExplSyntaxOff

\NewDocumentCommand{\testupper}{m}{%
  I am a \comparestringTF{#1}{upper}{TSAR}{tsar}%
}

In these examples you could use \NewExpandableDocumentCommand everywhere, but don't be too fast in employing it.

1
  • Thanks a lot. This should be the 1st question of a long series (I am switching all my packages to LaTeX3 and then I will put it on CTAN).
    – projetmbc
    Commented Jul 5, 2021 at 7:35
2

There isn't really a "LaTeX3" just an up to date LaTeX2e

\documentclass{article}

\NewDocumentCommand\test{s}{\IfBooleanTF{#1}{star}{no star}}

\begin{document}

\test

\test*

\end{document}

2
  • Thanks. That solves half of the problem. It remains to translate the string test.
    – projetmbc
    Commented Jul 5, 2021 at 7:20
  • 1
    @projetmbc oh sorry, I missed that: \str_if_eq:nnTF but I see egreg has already stolen the tick, that will make his day:-) Commented Jul 5, 2021 at 7:46
1

You'll want to use the xparse capabilities that have been part of LaTeX's kernel since last October. Your definition becomes:

\NewDocumentCommand{\test}{ s }{%
   I am a 
   \IfBooleanTF{#1}
     {TSAR}
     {tsar}
}

The full dirt on xparse can be had by typing texdoc xparse at a command line or searching for xparse at https://texdoc.org

1
  • Thanks. That solves half of the problem. It remains to translate the string test.
    – projetmbc
    Commented Jul 5, 2021 at 7:20

You must log in to answer this question.

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