TransWikia.com

LaTeX3 - Test string equality + Star version

TeX - LaTeX Asked on July 20, 2021

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

newcommandtest{@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

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.

Correct answer by egreg on July 20, 2021

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

documentclass{article}

NewDocumentCommandtest{s}{IfBooleanTF{#1}{star}{no star}}

begin{document}

test

test*

end{document}

Answered by David Carlisle on July 20, 2021

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

Answered by Don Hosek on July 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP