[Tex/LaTex] Quick and short command for coloring one word

macros

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?

Best Answer

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}
Related Question