7

I often make notes in engineering class using LaTeX, and have the need to color different single words on the fly. I use \textcolor{name}{text}, and often when using it rapidly find it quite cumbersome to rewrite even with autocomplete in hand.

I wish I had single short commands like \blue and \red etc or \blue{two words} if needing multiple words, likewise for red etc. But I'm not sure how to define a command as such.

Could someone explain it to me?

  • 4
    You could use something like that \newcommand{\red}[1]{\textcolor{red}{#1}} – Ilbant Nov 17 '17 at 9:13
13

The following MWE defines a new command \blue that takes one argument ([1]). The second pair of {} contains the definition or what the command should do. In this case, {text} is replaced by {#1}. That way, the argument that is given to \blue is used as the text that is colored by the command. In an analogous way, you can also define other commands for all the colors you need.

\documentclass{article}
\usepackage{xcolor}

\newcommand{\blue}[1]{\textcolor{blue}{#1}}

\begin{document}
\blue{word}  black colored text \blue{several words}

\end{document}

According to the request in the comments: It is also possible to define commands that change the color of the text without the need of parentheses. Nevertheless, one will need an additional command, that switches the color back to black.

\documentclass{article}

\usepackage{xcolor}

\newcommand{\red}{\color{red}}
\newcommand{\black}{\color{black}}

\begin{document}

text in black color \red red word \black normal text in black color

\end{document}
  • How about one with no parenthesis, like \blue followed-by-word? – E. l4d3 Nov 17 '17 at 11:23
  • @E.l4d3 Defining such a command that does not need parentheses is possible, however you will need a second command to switch the color back. I therefore doubt, that this approach saves you time. Additionally, using parentheses it is, at least to me, a lot clearer, to what text the color is applied. – leandriis Nov 17 '17 at 11:45
  • 1
    @leandriis: I don't think \newcommand can do it, but one could always do \def\red#1 {\textcolor{red}{#1} } -- though that wouldn't work for words at the end of a paragraph. – hmakholm left over Monica Nov 17 '17 at 17:24
  • @HenningMakholm: I must admit that I am not really sure what exactly you are refering to with 'I dont't think `\newcommand can do it...'. I have tested the command you provided in comparison to my example and couldn't encounter any different output. Could you please clarify? – leandriis Nov 17 '17 at 18:41
  • @leandriis: My definition ought to color exactly the next word red, without needing any bracing or subsequent \black. Notice that there is a space before the first {, delimiting the parameter. – hmakholm left over Monica Nov 17 '17 at 18:46
9

After activating with \shorthandon, red things can be delimited between @...@, blue with ^...^, and * can be used to get a literal of the next character, for example *@ for a @.

\shorthandoff disables the catcode redefs.

\documentclass{article}
\usepackage{xcolor}
\newcommand\shorthandon{\catcode`@=\active \catcode`^=\active \catcode`*=\active }
\newcommand\shorthandoff{\catcode`@=12 \catcode`^=7 \catcode`*=12 }
\shorthandon
\def@#1@{\textcolor{red}{#1}}%
\def^#1^{\textcolor{blue}{#1}}%
\def*#1{\string#1}
\shorthandoff
\begin{document}
\shorthandon
This is a @test@ of the ^emergency^ bro*@dcast system.
\shorthandoff
@*$x^2$
\end{document}

enter image description here

If one wanted the activating @ or ^ to color exactly one word thus alleviating the need for the trailing @ and ^, then the above approach can be easily adapted:

\documentclass{article}
\usepackage{xcolor}
\newcommand\shorthandon{\catcode`@=\active \catcode`^=\active \catcode`*=\active }
\newcommand\shorthandoff{\catcode`@=12 \catcode`^=7 \catcode`*=12 }
\shorthandon
\def@#1 {\textcolor{red}{#1} }%
\def^#1 {\textcolor{blue}{#1} }%
\def*#1{\string#1}
\shorthandoff
\begin{document}
\shorthandon
This is a @test of the ^emergency bro*@dcast system.
\shorthandoff
@*$x^2$
\end{document}

This produces the same output as the 1st MWE, but does not require a trailing code to signify the end of a colored block.

Obviously, with both these approaches, the activation characters (currently @, ^, and *) and their associated colors (currently red and blue) can be chosen to suit the particular needs of the OP.

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.