I'm trying to write LaTeX code in my document. Could someone help me with this error message:

LaTeX Warning: Command \textbackslash invalid in math mode on input line 4.

{\fontsize{10pt}{12.0pt}\selectfont $\textbackslash$ def$\textbackslash$ cp$ \{ $ $\textbackslash$ textit$ \{ $ cp.$ \} $ $ \} $ \ \par}\par

2 Answers 2


\textbackslash is a text-mode command, so it does not work in math-mode (as you may have noticed) because math fonts are wildly different from text fonts, so the characters are set up differently, so they are not at all compatible. in math-mode you'd use \backslash if you wanted the backslash math symbol.

Though your command:

$\textbackslash$def $\textbackslash$cp $\{$ $\textbackslash$ textit$ \{ $ cp.$ \} $ $ \} $

looks a lot like you are trying to typeset some LaTeX command. Then just use \verb:


enter image description here

As a rule of thumb: if it's too complicated, you're doing it the wrong way.

  • 2
    Your guess is extremely great...+1
    – MadyYuvi
    Feb 10, 2020 at 12:25
  • Thanks a lot. I am so fresh in LaTex that you can almost smell me still baking. I am trying to help my wife to convert source code to PDF file. She prepares thesis and she is stuck with converting source code to PDF
    – Tayco
    Feb 10, 2020 at 13:21
  • 1
    @Tayco You're welcome. Don't worry about being new to LaTeX: we all started somewhere :-) As I said, if it's too complicated (with too complicated I mean you have to drastically change the input to get what you want), then there is something wrong. You can easily write LaTeX code verbatim (no changes in the code). See here, for example: tex.stackexchange.com/q/49131/134574 and here: overleaf.com/learn/latex/Code_listing. And if you have any problems, feel free to ask a new question. Welcome to TeX.SX! Feb 10, 2020 at 13:25
  • It sounds so easy dear Phelype when you say it :) I used Overleaf and erro notification changed suddenly. Now it says Package microtype Warning: Redefining noligatures set `no ligatures' on input line 39.
    – Tayco
    Feb 10, 2020 at 13:35
  • @Tayco Oh, I know it's not easy. I was there not so long ago (I use LaTeX since 2015). But you get used to it. That is not an error, just a warning, so you can (usually) ignore it. In this case, the warning seems just to inform you, so you don't need to worry. Though when you have a problem, please ask here. Comments are not intended for lengthy discussions and might be deleted. Thanks! Feb 10, 2020 at 13:43

Welcome to TeX.SE... Why you need \textbackslash inside math, is there any intentional? You can use just \backslash inside math...

  • Thanks for asking. This is not a math work, this is text work.
    – Tayco
    Feb 10, 2020 at 12:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .