2
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}

%définition de variable texte pour compilation conditionnelle
\def\ChoixDeVersion{AB} %AB=Avec Barème, SB ou autre = sans bareme

\usepackage{ifthen}
\newcommand{\bareme}[1][2 ]{%
     \ifthenelse{\equal{\ChoixDeVersion}{AB}}%
     {#1 AB \ignorespaces}%
     {#1 \ignorespaces}%
}     
\begin{document}
\bareme{123 } 
\end{document}

The optional value of the first argument of the command \bareme is displayed all the time even if i give an argument when I call the command

\bareme{123 } gives

enter image description here

I would espect 123 AB. The value of the argument when I call the command (123) should take the place of the optional value (2 ) of the first argument (#1).

Where is my mistake ?

7
  • 3
    If there is just one (optional) argument, shouldn't it be \bareme[123 ]? – Johannes_B Nov 4 '17 at 14:28
  • I give first the number of argument [1] and then the default value of the first arguement [2] (2 is the default value). For me \bareme[123 ] would mean 123 arguments for the command \bareme – Idefix Nov 4 '17 at 14:35
  • Ahhh you mean for the call. Sounds strange to me, but that gives the result. Thank you so much. – Idefix Nov 4 '17 at 14:40
  • 1
    Strange? The syntax for optional argument has always been with brackets [...], not with braces {...} – egreg Nov 4 '17 at 14:53
  • Would it be possibe to make change(s) in a way to ge the value by \bareme{value} ? I would like to have the same syntaxe with the value \bareme{value}and default value \bareme{} – Idefix Nov 4 '17 at 18:46
2

Disclaimer: I don't recommend the misuse of the g or G optional argument specifier as {} usage.

LaTeX has only [] as optional argument for \newcommand etc. With g or G from xparse (which uses expl3 as 'upcoming' format of LaTeX 3) it is possible to apply \NewDocumentCommand and provide G{2} as optional argument that can be used with {} instead of [] -- 2 is the default value of the argument if it is not specified at all.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}

\usepackage{xparse}

%définition de variable texte pour compilation conditionnelle
\def\ChoixDeVersion{AB} %AB=Avec Barème, SB ou autre = sans bareme

\usepackage{ifthen}
\NewDocumentCommand{\bareme}{G{2}}{%
     \ifthenelse{\equal{\ChoixDeVersion}{AB}}%
     {#1 AB \ignorespaces}%
     {#1 \ignorespaces}%
}     
\begin{document}
\bareme{123} 

\bareme
\end{document}

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.