Site Tools

latex_macro_single_optional_argument

Differences

This shows you the differences between two versions of the page.

 — latex_macro_single_optional_argument [2019/07/24 14:34] (current)60.240.96.191 created 2019/07/24 14:34 created 2019/07/24 14:34 created Line 1: Line 1: + ====== Single Optional Argument for a LaTeX Macro ====== + I recently need to create a new macro for a User manual. The macro was to output a defined character, but in a colour specified by the author. Therefore, I needed a macro that accepted a single optional argument. + + After doing a lot of searching around, I could only find advice on two or more arguments. I needed this: + + + Print my symbol in black \specialsymbol please. + Print my symbol in red   ​\specialsymbol[red] please. + Print my symbol in blue  \specialsymbol[blue] please. + ​ + + The macro is: + + + \newcommand{\specialsymbol}[1][black]{% + \begin{myfont}% + \textcolor{#​1}{}% backtick character contains the symbol in the "​myfont"​ font. + \end{myfont}% + }% + ​ + + And for extra niceness, if you want your macro to ensure text following the symbol does not butt up hard against it, change line three to: + + + \textcolor{#​1}{}\ % +