7

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
Tayco
  • 71

2 Answers2

12

\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:

\documentclass{article}
\begin{document}
\verb|\def\cp{\textit{cp.}}|
\end{document}

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 '20 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 '20 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: https://tex.stackexchange.com/q/49131/134574 and here: https://www.overleaf.com/learn/latex/Code_listing. And if you have any problems, feel free to ask a new question. Welcome to TeX.SX! – Phelype Oleinik Feb 10 '20 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 '20 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! – Phelype Oleinik Feb 10 '20 at 13:43
  • For converting source code to pdf, look into the listings package. Or minted. – Teepeemm Feb 11 '20 at 04:50
3

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

MadyYuvi
  • 13,693