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.

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

share|improve this question
4  
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
3  
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
3  
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
show 1 more comment

2 Answers

up vote 12 down vote accepted

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:

enter image description here

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}
share|improve this answer
add comment

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

\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}
share|improve this answer
add comment

Your Answer

 
discard

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.