# How can I change the color of a command in Latex?

For example, I want to type \this as a regular text,but I want it to be blue. I tried \textcolor{red}{\this} but it doesn't work.To specify,I want the backslash included and colored.

• Do you want to change the color in the output pdf file or do you want to change the color in the editor? – leandriis May 9 '20 at 11:13
• If you want to change the color in the pdf file, use \textcolor{red}{this}. – leandriis May 9 '20 at 11:13
• If you want to change the color in the editor, please specify which editor you use. – leandriis May 9 '20 at 11:13
• In order to use \textcolor successfully, you need to load the xcolor package. – Mico May 9 '20 at 11:14
• I am sorry if I was not clear enough, I have just started using Latex so let me clear some things up, I would like to change the color in the pdf file, but I want to type a command as a normal text and have it colored, (ex. "\somethingsomething" but in blue). – Harun Duranović May 9 '20 at 11:21

Here are two versions. The second version is under the assumption that "\that" is actually some code that you want to include verbatim into your pdf output while also coloring it.

\documentclass{article}
\usepackage{xcolor}

\usepackage{listings}

\lstdefinestyle{mystyle}{basicstyle=\rmfamily\color{blue}}

\begin{document}
some text \textcolor{blue}{\textbackslash this}

some text \lstinline[style=mystyle]{\this}
\end{document}


Perhaps something like this is what you're after?

\documentclass{article}
\usepackage{xcolor}

\def\this{\textcolor{blue}{this}}
\begin{document}
Do you mean like \this?

\end{document}


If you wish the to include the backslash, you could use

\def\this{\textcolor{blue}{\textbackslash this}}

• I would like to have the backslash included and colored like the rest of the text. – Harun Duranović May 9 '20 at 11:30
• Is there a chance that "\this" is some actual code of any kind that you would like to see in your pdf as it is (but additionally colored)? – leandriis May 9 '20 at 11:40
• As far as I know it's not, but when I start typing anything with \ it always recognizes it as a command. – Harun Duranović May 9 '20 at 11:43
• I see; I've edited my answer, but leandriis' question is a good one and might warrant a different answer. – Fintan May 9 '20 at 11:45
• Yes, it worked , thanks a lot! And sorry if the question wasn't clear enough, as I said, I'm a beginner and I'm new to all of this. – Harun Duranović May 9 '20 at 11:49