# Making a conditional statement in LaTeX

I am trying to make a style file for me to simplify note-taking, especially subjects involving Math.

I want to make the \delim command, having one mandatory argument for the expression, and an optional argument to change the delimiter using the characters from the matrix commands from amsmath, which are (p), [b], {B}, |v|, ||V|| with p as the default.

I think it will involve \if commands, but I don't know how to check if the optional argument is p, b, B, v, or V. Any help is much appreciated.

(Disclaimer: My personal opinion is that while this might make typing faster, it renders the code far less readable.)

Here is a way: first I define a bunch of paired delimiters by means of \DeclarePairedDelimiter, which provides for all of them a stared version which scales automatically, and an optional argument to give the size manually; then I introduce the user macro which selects the desired delimiters through an optional argument

\documentclass{article}

\makeatletter
\newcommand*{\delim@}{\delim@p}% better safe than sorry
\DeclarePairedDelimiter{\delim@p}{(}{)}
\DeclarePairedDelimiter{\delim@b}{[}{]}
\DeclarePairedDelimiter{\delim@B}{\{}{\}}
\DeclarePairedDelimiter{\delim@v}{|}{|}
\DeclarePairedDelimiter{\delim@V}{\|}{\|}
\newcommand*{\delim}[1][p]{\csname delim@#1\endcsname}
\makeatother

\begin{document}

$\delim{a+b}, \delim[b]{a+b}, \delim*{\frac{1}{2}}, \delim[B]*{\frac{1}{2}}, \delim[v][\Bigg]{\frac{1}{2}}, \delim[][\Big]{a+b},% or \delim[p][\Big]{a+b} \delim[F]{a+b}$

\end{document}


The first optional argument is p, b, and so on. Then you can write a star for automatic scaling, or give the manual size as further optional parameter.

Check out the last two cases: if you want to give only the manual size with round brackets then the first optional parameter should either be empty or be an explicit p. Furthermore, there is no test on p, b, ... If you misspell it, nothing meaningful comes out: \delim[F]{a+b} gives just a+b.

If you really want to see how a bunch of nested \ifs would look like, you could do

\newcommand*{\delim}[1][p]{%
\def\@tempa{#1}%
\if#1p\relax
\else
\if#1b\relax
\else
\if#1B\relax
\else
\if#1v\relax
\else
\if#1V\relax
\else
\def\@tempa{p}% if none of pbBvV then fall back to p
\fi
\fi
\fi
\fi
\fi
\csname delim@\@tempa\endcsname
}


which would give parentheses in case of a misspelled/empty argument.

• Is there a way that is something like if arg = p then? Your MWE is beyond my expectations, but I can't understand it quite well. Mar 26, 2021 at 16:41
• @soupless I'm not sure I understand the question. It would be possible to write a bunch of nested \ifs but I thought this to be a more efficient approach. Mar 26, 2021 at 16:44
• Yes, that is exactly what I kind of expected: a bunch of nested \ifs. Can you give a simple example, like only for p and b? If so, kindly add it in your answer. Just starting so I kind of sticks to the familiar ways :) Mar 26, 2021 at 16:46
• It makes sense now that it is inefficient, but this will be a learning opportunity for me. Mar 26, 2021 at 16:59
• I forgot to ask this question, but is it okay to write \if#1p as \if{#1}{p}? Mar 27, 2021 at 10:14