User Tools

Site Tools


latex_macro_single_optional_argument

Differences

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

Link to this comparison view

latex_macro_single_optional_argument [2019/07/24 14:34] (current)
60.240.96.191 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:
 +
 +<code latex>
 +Print my symbol in black \specialsymbol please.
 +Print my symbol in red   ​\specialsymbol[red] please.
 +Print my symbol in blue  \specialsymbol[blue] please.
 +</​code>​
 +
 +The macro is:
 +
 +<code latex>
 +\newcommand{\specialsymbol}[1][black]{%
 +\begin{myfont}%
 +\textcolor{#​1}{`}% backtick character contains the symbol in the "​myfont"​ font.
 +\end{myfont}%
 +}%
 +</​code>​
 +
 +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:
 +
 +<code latex>
 +\textcolor{#​1}{`}\ %
 +</​code>​
latex_macro_single_optional_argument.txt ยท Last modified: 2019/07/24 14:34 by 60.240.96.191