User Tools

Site Tools


latex_macro_single_optional_argument

Single Optional Argument for a LaTeX Macro

I recently needed 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}{`}\ %
latex_macro_single_optional_argument.txt ยท Last modified: 2021/02/02 01:24 (external edit)