This shows you the differences between two versions of the page.
latex_macro_single_optional_argument [2019/12/09 21:58] 210.18.207.206 |
latex_macro_single_optional_argument [2021/02/02 01:24] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 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: | ||
- | |||
- | <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> |