How to Add Conditional Formatting in a \newcommand
At a glance
- 1. Choose a test value
Decide which argument value should trigger the special formatting.
- 2. Load the package
Load ifthen for text comparisons and xcolor if the formatting uses colors.
- 3. Write both branches
Put the special formatting in the true branch and the default formatting in the false branch.
- 4. Test the macro
Call the command with matching and nonmatching values to check both outcomes.
You can put a condition directly inside a \newcommand definition. The macro checks one of its arguments and chooses which formatting to apply. For a simple comparison of text labels, the ifthen package provides \ifthenelse and \equal.
A complete example
This macro takes a status and a message. It prints urgent messages in bold red and other messages in blue.
\documentclass{article}
\usepackage{xcolor}
\usepackage{ifthen}
\newcommand{\statusmessage}[2]{%
\ifthenelse{\equal{#1}{urgent}}%
{\textcolor{red}{\textbf{#2}}}%
{\textcolor{blue}{#2}}%
}
\begin{document}
\statusmessage{urgent}{The server is down.}
\statusmessage{normal}{The scheduled update is complete.}
\end{document}
The first argument, #1, is the value being tested. The second argument, #2, is the message to format. When #1 is urgent, the condition is true and the first branch is used; otherwise, the second branch is used. The % characters prevent line breaks and indentation in the definition from adding unwanted spaces to the output.
How the conditional works
The general form is:
\ifthenelse{<condition>}{<true branch>}{<false branch>}
In a macro definition, #1, #2, and so on refer to the macro’s arguments. For example, \equal{#1}{urgent} compares the first argument with the text urgent. Put the formatting commands for a match in the true branch, and the ordinary or alternative formatting in the false branch.
The example uses \textcolor and \textbf from xcolor. The condition itself comes from ifthen; if your branches only change font style, you may not need xcolor.
Adapting the pattern
To test a different argument, change the number sign and argument number. For instance, in a three-argument command, \equal{#2}{warning} tests the second argument against warning.
You can also use the condition to select different text, not just different styling:
\newcommand{\availability}[1]{%
\ifthenelse{\equal{#1}{yes}}%
{Available}%
{Unavailable}%
}
This works well for a small set of simple text labels. If you want to handle several possible labels, you can nest conditionals, but a long chain quickly becomes hard to maintain. For many cases, define separate commands or use a dedicated key-value interface instead.
Common mistakes
- Leaving out a branch:
\ifthenelseneeds a condition, a true branch, and a false branch, in that order. - Comparing the wrong argument: In a definition with two arguments,
#1is the first and#2is the second. Check the command’s argument order. - Expecting a numeric comparison:
\equalis for comparing text, not for testing whether a number is greater than another. Use a numeric conditional suited to the values and packages in your document. - Testing formatted output instead of the input: Put the test on the argument value, then apply formatting in the selected branch. This keeps the condition separate from the appearance.
- Forgetting package dependencies:
\ifthenelseand\equalrequireifthen;\textcolorrequires a color package such asxcolor.
For a simple conditional based on an argument, this pattern keeps the decision and the formatting together in one command. If your macro needs many options or complicated comparisons, a more structured interface will usually be easier to extend.
References
- The ifthen package
- The LaTeX2e unofficial reference manual
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.