3

I am using the following command to color some text:

{\color{NavyBlue}

Text Here!

}

Because I used it frequently I created this macro:

\def\cl#1{{\color{NavyBlue}{#1}}}

When I am using the first then I can have blank lines between the {} and the text meaning that I can do this:

{\color{NavyBlue}
%Blank line
%Blank line
Text!
%Blank line
%Blank line
}

On the other hand when I use the defined macro I can't do this as an error occurs saying "Paragraph ended before \cl was complete".

\cl
{
%Blank line
Text Here!
%Blank line
}

This is not a question regarding a solution on how to use the defined macro, as when I don't leave blank lines then it works fine. What I am asking is why is that happening and how can I define the macro to act like the command {\color{}}.

MWE

\documentclass{article}

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

\def\cl#1{{\color{NavyBlue}{#1}}}

\begin{document}

Text here

\end{document}
  • 2
    Try \newcommand\cl[1]{...} and live happy. – egreg Oct 30 '14 at 0:15
  • 2
    Or \long\def\cl#1{{\color{NavyBlue}#1}} to allow a paragraph break. Also \color{NavyBlue}{#1}} should be \color{NavyBlue}#1} ie, no braces around #1. – user11232 Oct 30 '14 at 0:16
  • \newcommand is better in LaTeX, though, as it takes precautions which you don't get with \def. – cfr Oct 30 '14 at 0:17
3

When you use \def the arguments to the macro you define cannot contain \par (or a blank line, that's the same). This is a feature Knuth added in order to catch “runaway arguments”, that is, a forgotten closing brace around an argument.

There is an escape from this, and indeed LaTeX by default doesn't use this feature in its \newcommand function.

So with \newcommand you don't have the problem. However, your definition is not really safe:

\newcommand{\cl}[1]{{\leavevmode\color{NavyBlue}#1}}

would be much better. Note that the braces around #1 are not necessary.

Moral of the story: don't use \def unless you know what you're doing and you've read chapter 20 of the TeXbook at least twice. ;-)

7

\def is not a latex command, you should use \newcommand and then blank lines (\par) are allowed in the argument. \long\def would also work, but isn't latex either.

But you may want to use

\textcolor{NavyBlue}{#1}

rather than

{\color{NavyBlue}{#1}}

as \color at the start of a paragraph can act in undesirable ways.

  • Thank you for your answer! it is really great. Although I chose egreg's as it is an answer best suited to my problem. :) – Adam Oct 30 '14 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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