4

My issue is that I'm trying to write variable names in italic, but as some of them have underscores in their names, LaTeX assumes that I am writing an equation and it messes up the rest of my document after that point. Here is a sample of my code:

As variable \textit{sensor.linear_acceleration} is a matrix

The underscore in linear_acceleration is what makes my whole document math modded, and is a matrix renders as if it was an equation.

How can I display that underscore in italic without LaTeX making everything an equation?

3
  • 1
    If these are variable names in the programming sense rather than the math sense, it'd probably be more conventional to use fixed-width font instead of italics. (And in some circles it's common to write long multi-character math variable names in upright font.) But that's a separate matter.
    – David Z
    Commented Jul 20, 2020 at 19:53
  • 1
    In relation to above comment, moreover with \verb| ...| you won't need to modify the variable names, but be aware that this is special command that cannot be used anywhere (for instance, as part of a macro definition or a macro argument).
    – Fran
    Commented Jul 21, 2020 at 6:43
  • Now that you say it, fixed-width fonts are indeed better for variable names, as these are programming variables. I've used the courrier package and it looks more convincing.
    – Jean S
    Commented Jul 23, 2020 at 12:22

2 Answers 2

14
\documentclass{article}
\begin{document}

As variable \textit{sensor.linear_acceleration} is a matrix,

\end{document}

will result in multiple error messages upon trying to compile such as:

! Missing $ inserted and Extra }, or forgotten $.

Also the corresponding entry in the .log file explicitly warns:

I've inserted a begin-math/end-math symbol since I think you left one out. Proceed, with fingers crossed.

As soon as you recieve any error message, do not look at the pdf at all, but try to fix the errors first. Even if you get something that on first glance resembles a pdf file, there can still be issues with it. After an error, TeX only tries to recover enough to syntax check more of the file, it does not try to make sensible output after an error. Once your document compiles successfully (does not generate a single error message), you can have a look at the pdf and see if the output suits your needs.


How to correct the above code entirely depends on the expected output. If you want the _ to be printed as it is, use \_ instead. If you want the following text following the _ to appear as a subcscript, use \textsubscript instead:

\documentclass{article}
\begin{document}

As variable \textit{sensor.linear\_acceleration} is a matrix,


As variable \textit{sensor.linear\textsubscript{acceleration}} is a matrix,

\end{document}

enter image description here

1
  • 1
    Thanks a lot ! This is simpler than I thought I'm still new to LaTeX so it's not rare for me to compile with a few errors, I'll try to correct everything before even looking at my PDF in the future. Sincerly,
    – Jean S
    Commented Jul 20, 2020 at 8:16
7

Yes, the underscore character is syntactical to (La)TeX meaning "subscript". (La)TeX complains if you have it outside of math mode and will switch to math mode (and give an error message) when you use it in regular text. You probably want to use \_ instead (already answered by @leandris). But I have to mention the "underscore" package, which allows you to use _ to print an underscore in text mode, but signal a subscript in math mode. That underscore also allows LaTeX to hyphenate the words it joins.

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