2

Maybe someone can tell me how to define a new command in \LaTeX, which can take one mandatory value and another optional value (parameter) as input. In this case, depending on the value of the optional argument, the type of the output object should change.

Specifically in my case, I need to specify a command \dependSet(<arg>, <opt par>) to draw parentheses. in this case, depending on the presence / absence of the optional argument should change the output (presence / absence of a vertical line inside):

Function Output
\dependSet{x, y} enter image description here
\dependSet{x, y}{a} row

Accordingly, for usability it is important that the parameter comes after the arguments of the command, and it is selected with curly brackets, not square ones.

P.S.: Actually, it's a pity that LaTex uses a rather uncomfortable notation (which reeks of something like ALGOL 60) and does nothing to make it possible to define new commands in Python-like style.

9
  • Did you check en.wikibooks.org/wiki/LaTeX/Macros and ctan.org/topic/macro-supp ?
    – MS-SPO
    May 12, 2022 at 9:31
  • optional argument would be in [] not {} but with regard to your PS do you mean optional named arguments as in the standard \includegraphics[width=2cm,angle=90]{filename} ? May 12, 2022 at 9:33
  • 1
    @Sssur sorry, but that syntax isn't clear, it is problematic. If you have an optional argument use the conventional style of optional arguments, that is []. I don't see how your \dependSet{x}{t} should be clearer than \dependSet{x}[t].
    – Skillmon
    May 12, 2022 at 11:03
  • 1
    And if you want an easy way to set up a command with key=value syntax, there are solutions for that (I think my expkv-cs does a pretty good job providing an easy interface with \ekvcSplit, though it isn't as flexible as Python would be (no positional arguments, just kw)).
    – Skillmon
    May 12, 2022 at 11:06
  • 1
    IMHO, most clear syntax is: (x,y) or (x,y|a). First case needs 5 characters, second case needs 7 characters in our source file. On the other hand you suggest syntax \dependSet{x,y} or \dependSet{x,y}{a}. First case needs 15 characters and second case 18 characters in your source file. Why this complication? We can do a macro which transforms a complicated syntax to a clear syntax, but why to do it?
    – wipet
    May 12, 2022 at 15:28

2 Answers 2

9

Let's assume you don't want to type in (x,y) or (x,y\mid a) for some reasons. And there are good reasons not to: for instance, you might want or be required to change notation. A hardwired input would make the task very difficult.

The idea is to type in

\dependSet{x,y}
\dependSet{x,y|a}

but without impacting on the final output. The input is quite natural and avoids the complications of too many braces.

How do we do it? We can use \NewDocumentCommand and its \SplitArgument feature.

\documentclass{article}
%\usepackage{xparse} % uncomment for older versions of LaTeX

\NewDocumentCommand{\dependSet}{>{\SplitArgument{1}{|}}m}{%
  \dependSetAux#1%
}
\NewDocumentCommand{\dependSetAux}{mm}{%
  \IfNoValueTF{#2}{% no | found
    (#1)%
  }{% found |
    (#1\mid #2)%
  }%
}

\begin{document}

$\dependSet{x}+\dependSet{x,y}+\dependSet{x,y|a}$

\end{document}

enter image description here

Comments.

The command \dependSet passes its argument to \dependSetAux as two braced groups. If there is no | in the initial argument, the second braced group will be {-NoValue-} that can be tested as shown.

If you want to change the final formatting, act on \dependSetAux.

A possibly more efficient code, but less straightforward, would be

\NewDocumentCommand{\dependSetAux}{mm}{%
  (#1\IfValueT{#2}{\mid #2})%
}
4
  • 1
    Shouldn't the \dependSetAux not be defined via \NewDocumentCommand, after all it is an auxiliary, not at the document level.
    – Skillmon
    May 13, 2022 at 5:54
  • 1
    @Skillmon The auxiliary command might need preprocessing itself, and it’s more friendly doing like this rather than going \ExplSyntaxOn anyway, for such a simple task.
    – egreg
    May 13, 2022 at 7:49
  • Thanks a lot! True, a natural question arose (which I had not even thought about before) how do \def, \newcommand, \NewDocumentCommand etc differ?
    – Sssur
    May 14, 2022 at 5:33
  • @Sssur \def is dangerous; \newcommand has just a few features; \NewDocumentCommand has many, see texdoc xparse
    – egreg
    May 14, 2022 at 7:15
4

The following code is fully compatible with your specification, given by complete example:

\documentclass[onecolumn]{article}

\newcommand\ds[1]{%
    (#1)%
}

\begin{document}
    This can be used as text:
    
    \ds{x, y}
    
    \ds{x, y|a}
    
    It can also be used inline in math-mode: $\ds{x, y}$, see also: $\ds{x, y|a}$.
\end{document}

As you can see, the idea is to separate argument from functionality. This is easier than thinking of all kinds of combinations you may or may not want to use.

enter image description here

0

You must log in to answer this question.

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