Sunday, 2 June 2013

\newcommand argument confusion

\newcommand argument confusion

I'm trying to work through LaTeX Beginner's Guide by Stefan Kottwitz. Here's where I got stumped:
\documentclass{article}
\newcommand{\keyword}[2][\bfseries]{{#1#2}}
\begin{document}
\keyword{Grouping} by curly braces limits the
\keyword{scope} of \keyword[\itshape]{declarations}.
\end{document}
The output adds boldface to "Groupings" and "scope," while "declarations" is italicized. I do not understand this syntax. I vaguely get that \bfseries is the "default" and \itshape got specifically subbed in for "declarations" but I cannot follow Kottwitz's explanation:
Let's look again at the bold marked line in the code. By using [\bfseries], we introduced an optional parameter. We refer to it with #1. Its default value is \bfseries. Since we used a declaration this time, we added a pair of braces to ensure that only the keyword is affected by the declaration. Later in the document, we gave [\itshape] to \keyword, changing the default formatting to italics.
Can I get a second opinion/explanation of what is going on here?

No comments:

Post a Comment