# Creating my first command

I want to create a command that will format code in a certain way. In this case I want code to be italic and red. So instead of doing \textcolor{red}{\textit code} I want to make a function, if you will, to do it for me with less lines of code. My understanding of it so far is very... basic. I just started using latex, this is what I have so far.

\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\newcommand{\code}[1]{\textcolor{ForestGreen}{\textit #1}}


But this only prints out the first letter in italic. The colour seems fine though. Any help and explanations would be wonderously helpful.

-
Perhaps this is what you want: \newcommand{\code}[1]{\textcolor{ForestGreen}{\textit{#1}}}? Also, no need to load color and xcolor; it's enough to load xcolor. – Gonzalo Medina Jun 7 '12 at 21:37
Remember that \textit requires an argument: you want to say \textcolor{red}{\textit{code}} in your original method. And you need to say \code{code}, not \code code. – egreg Jun 7 '12 at 21:40
In addition to the above two comments I would suggest using creating the macro with two parameters and making the first one optional to specify the color (and default it to ForestGreen): \newcommand{\code}[2][ForestGreen]{\textcolor{#1}{\textit{#2}}}. The \code{text} will produce italics in ForestGreen, and \code[red]{text} will produce italics in red. – Peter Grill Jun 7 '12 at 21:44
Thanks guys, that was an easy fix :). Also thanks for the tip to remove colors package, indeed it is not needed! – Dan Jun 7 '12 at 21:46
Also Greg you make a good point, I guess I didn't notice I forgot to add another set of curly braces. – Dan Jun 7 '12 at 21:48

Expanding on the comments of Gonzalo Medina and egreg, I would recommend create the macro \code as such:

    \newcommand{\code}[2][ForestGreen]{\textcolor{#1}{\textit{#2}}}


Then \code{text} and \code[red]{text} produce:

## Explanation:

• [2] so as to accept two parameters: #1 and #2.
• To provide a default color, we make the first one (#1) optional via defining a default of [ForestGreen].

## Notes:

• no need to load both color and xcolor packages -- just load xcolor.

## Code:

\documentclass{article}
\usepackage[svgnames]{xcolor}

\newcommand{\code}[2][ForestGreen]{\textcolor{#1}{\textit{#2}}}

\begin{document}
\code{text}

\code[red]{text}

\end{document}

-

Well, most has already been commented while I was composing an answer. I recommend that you have a look at the xparse package -- using it considerably simplyfies command definitions as soon as optional parameters and conditional branches are involved. You can find the documentation on CTAN, an example is included in the code given below.

\documentclass{article}
\usepackage{lipsum}
\usepackage{xparse}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\newcommand{\format}[1]{%
\textcolor{ForestGreen}{\textit{#1}}}

%format multiple paragraphs
\newcommand{\parformat}[1]{%
{\color{ForestGreen} \itshape #1}}

%xparse version; [m]andatory text parameter,
%[O]ptional color parameter with a given default
\NewDocumentCommand{\xformat}{O{MidnightBlue} m}{%
\textcolor{#1}{\textit{#2}}}

%xparse version for long mandatory argument
\NewDocumentCommand{\xparformat}{O{MidnightBlue} +m}{%
{\color{#1} \itshape #2}}

\begin{document}

\noindent
\format{Formatted text}\\
\xformat{some more text}\\
\xformat[red]{yet more text}\\
\parformat{\lipsum[3]\par\lipsum[4]}
\xparformat[cyan]{\lipsum[3]\par\lipsum[4]}

\end{document}

-