Quick and short command for coloring one word
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}
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}
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.