Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.


\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.

share|improve this question
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

2 Answers 2

up vote 12 down vote accepted

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


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

enter image description here


  • [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].


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






share|improve this answer

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.

enter image description here



%format multiple paragraphs
    {\color{ForestGreen} \itshape #1}}

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

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


\format{Formatted text}\\
\xformat{some more text}\\
\xformat[red]{yet more text}\\

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.