51

I have a set of commands in my document's preamble that look like this:

\newcommand{\nathaniel}[1]{\textcolor{blue}{[#1]}}
\newcommand{\anotherAuthor}[1]{\textcolor{darkgreen}{[#1]}}

They are to allow my coauthors and I to make comments in the document that will appear in the pdf, to facilitate working on it. However, we find we quite often want to put several paragraphs inside such a comment (e.g. if we've written some new material that we're not sure about, and we want the other authors to notice it). If I try to do this:

\nathaniel{
    one

    two
 }

then I get this error:

paragraph ended before \@textcolor was complete

I know I can use \\ to get a linebreak instead, but I'd prefer it if my custom command would just accept normal paragraph breaks. Is there some command that will cause LaTeX to accept multiple paragraphs as if they were a single one, or some other way to colour multiple paragraphs of text?

N. Virgo
  • 4,289
  • 2
  • 26
  • 41

7 Answers7

43

\textcolor is similar to \mbox, so it doesn't typeset paragraphs; use the declarative form, instead, in a group:

\newcommand{\nathaniel}[1]{{\leavevmode\color{blue}[#1]}}
egreg
  • 1,121,712
  • 1
    What's the purpose of \leavevmode here? (And why is it within the braces?) https://tex.stackexchange.com/a/22853/ says that \leavevmode is to avoid paragraph-break problems, but when I omit \leavevmode I don't see paragraph-break problems. – root Jan 06 '19 at 20:08
  • @root It's best not to have \color between paragraphs, so this implementation adds \leavevmode in order to start the paragraph before setting the color. Inside or outside the braces is immaterial. – egreg Jan 06 '19 at 20:19
  • 2
    @root No, I mean if \nathaniel comes after a blank line. If issued between paragraphs, \color inserts an object that can affect pagination. – egreg Jan 06 '19 at 20:27
  • 2
    it would be better to change [#1] to {#1} so as to remove the bracket appearing in the blue texts. – Mingming Qiu Jun 23 '23 at 02:02
  • 1
    @MingmingQiu The brackets are wanted by the OP. – egreg Jun 23 '23 at 09:06
13

you could make an environment form

\newenvironment{thisnote}{\par\color{blue}}{\par}

then

\begin{thisnote}
   stuff

   in blue
\end{thisnote}

can take paragraphs of text

David Carlisle
  • 757,742
  • I'm always unsure about this. The way it is defined here, the \endgroup triggering color-back-switching will come in vertical mode, hence the switching magic will go in the vertical list, making \lastbox and things more difficult. Hence I always try to have color switching in horizontal mode. – Stephan Lehmke Mar 29 '12 at 10:01
  • 1
    true but arranging environment switching to be always in hmode is a pain too (see missing item error question in today's list) When we added the colour support did experiment with a version that always delayed itself if used in hmode, and insert at start of next para but that complication had downsides as well as it broke more 3rd part packages and is harder to make work with rules and things. – David Carlisle Mar 29 '12 at 10:18
12

This is what I use:

\newcommand{\newstuff}[1]{\color{blue}{#1}\color{black}}
Andreina
  • 987
  • 7
    Great! Probably even better to put \color{blue} into the same braces as #1 and remove \color{black}, for example in case black is not default: \newcommand{\newstuff}[1]{{\color{blue}#1}} – root Jan 06 '19 at 17:46
  • 1
    You could add \AtBeginDocument{\colorlet{default}{.}} to the preamble and replace \color{black} by \color{default} for cases where the default color is set to something different than black. – Jasper Habicht Sep 21 '22 at 23:45
2

Just for a change:

\documentclass{article}  
\usepackage{xcolor}
\usepackage{lipsum}
%%---------------------------- colored box as paragraph---------------
\newcommand*{\mybox}[2]{%
\noindent\colorbox{#1!30}{%
\parbox{0.98\linewidth}{#2}}}%
%%----------------------------colored text as paragraph---------------
\newcommand*{\mytext}[2]{%
\noindent\color{#1!80}{%
\parbox{0.98\linewidth}{#2}}}%
%%----------------------------
\begin{document}
%=====================
\mybox{green}{\lipsum[1]}
\par
\mytext{red}{\lipsum[2]}   
%%----------------------------
\end{document}

Disclaimer: This won't break across pages. If that is the requirement, one can use mdframed package.

doncherry
  • 54,637
2

I'd like to highlight the solution given by @root in the comments:

\newcommand{\mybluecolor}[1]{{\color{blue}#1}}

For example, this works just fine:

{\color{blue}
Paragraph 1 ...

Paragraph 2 ... }

  • Just to note this is essentially the same as the accepted answer. (But that one has a \leavevmode to prevent edge cases involving pagination.) – N. Virgo Apr 12 '22 at 00:29
0

Medium experience user here, I am working on Overleaf but it should work on your setup too. Merging the answers from "root" and "doncherry" I made one that worked for me and I love it, here you go... enjoy:

\documentclass{article}  
\usepackage[dvipsnames]{xcolor} %permits using colors for text, to check all the colors that are available with "dvipsnames" check https://www.overleaf.com/learn/latex/Using_colours_in_LaTeX to know what to choose as the "color name"
\usepackage{lipsum}

\newcommand{\multilinecolortext}[2]{\color{#1}{#2}} \begin{document}

%The number after the "!" indicates the opacity of your text, so that way we can control the transparency. 0 means is fully transparent and 100 means is fully opaque. {\multilinecolortext{Green!80}{My first dad-joke goes like this:\ A girl comes to her dad and says Dad, make me a sandwich". So he taps her head and says:Puff! You're a sandwich! ... XD

Ok since you liked, it here is another joke to exemplify multiple lines, :

\noindent I went on a one-in-a-lifetime vacation... never again}}

This is Plum color text at 25% opacity:

{\multilinecolortext{Plum!25}{\lipsum[2]}}

This is Bittersweet color text at 50% opacity:

{\multilinecolortext{Bittersweet!50}{\lipsum[2]}}

This is Turquoise color text at 75% opacity:

{\multilinecolortext{Turquoise!75}{\lipsum[2]}}

This is CadetBlue color text at 100% opacity:

{\multilinecolortext{CadetBlue!100}{\lipsum[2]}} \end{document}

just like that you will have something that looks like this:

enter image description here

0

A nasty solution, but it may be suitable for some specific purposes. Rather than the usual line break with \\, use \\ \phantom{a} \\ combination to break the line inside the \color block. For example, -

\textcolor{red}{paragraph 1 \\ \phantom{a} \\
 %
 paragraph 2 \\ \phantom{a} \\
 % 
 paragraph 3
 }

The output is -

enter image description here

Note that this will not add indentation before the new paragraph. But you can also add that manually.