LaTeX Inline Code: texttt, verb and lstinline Compared
November 11, 2026 · 9 min read
LaTeX Inline Code: \texttt, \verb and \lstinline Compared
You write LaTeX inline code with one of three commands. Use \texttt{...} for short names, \verb|...| for snippets full of special characters, and \lstinline when your document already loads the listings package. Choose by what the snippet contains and where it sits, because each command fails in a different place.
How Do You Insert Inline Code in LaTeX?
To put code inside a sentence, wrap it in one of three commands. All of them print in a typewriter font, but they treat your characters very differently. This table sums up the trade-offs we tested with pdfLaTeX in TeX Live 2025.
\texttt | \verb | \lstinline | |
|---|---|---|---|
| Package | None, built in | None, built in | listings |
| Special characters typed as-is | No, escape them | Yes | Yes |
Inside \section, \footnote, \caption | Yes | No, it errors | Works, but experimental |
| Breaks across lines | At spaces only | Never | With breaklines=true |
| Language-aware styling | No | No | Yes, with language= |
A quick rule of thumb follows from the table:
- A variable, file or function name in running text:
\texttt. - A one-off snippet packed with
#,%,\or{:\verb. - A document that already shows code blocks with listings:
\lstinline, so inline and block code match.
We prefer \texttt for identifiers and short names. It works everywhere, including headings and captions, and the escaping cost is small for a name like max_len. Block code is a different job. It belongs in an environment, not in a sentence.
\texttt: Typewriter Font, Not Verbatim
\texttt only switches the font. LaTeX still reads everything inside it as LaTeX, so commands run, special characters stay special and runs of spaces collapse to one. That makes it safe inside any other command, and also the reason you have to escape.
Call \texttt{solve(k, q, dx)} once per time step.
The settings live in \texttt{config\_file.yaml}.
That prints "Call solve(k, q, dx) once per time step." and "The settings live in config_file.yaml." with both snippets in the typewriter font. Leave out the backslash before the underscore and pdfLaTeX stops with Missing $ inserted, because _ is a subscript character that only works in math mode.
Two more surprises catch people. A \\ inside \texttt is a line break, not a printed backslash. A bare ~ is a non-breaking space, so \texttt{~/x} prints a gap followed by /x.
How Do You Type ~, \ and _ in LaTeX Inline Code?
Inside \texttt, each of LaTeX's ten reserved characters needs its own escape. The LaTeX2e reference's text symbols page lists the named commands for the three that have no short form.
| Character | Write this inside \texttt |
|---|---|
# | \# |
$ | \$ |
% | \% |
& | \& |
_ | \_ |
{ and } | \{ and \} |
~ | \textasciitilde{} |
^ | \textasciicircum{} |
\ | \textbackslash{} |
The empty braces after the named commands stop them from swallowing the space that follows. Inside \verb you skip all of this and type every character raw, which is the whole point of the command.
Add \usepackage[T1]{fontenc} to your preamble as well. Under LaTeX's default OT1 encoding, \textbackslash and \{ inside \texttt borrow their glyphs from another font, so they don't match the code around them. OT1 also turns a plain < and > in body text into the inverted marks ¡ and ¿. With T1 loaded, the backslash and braces come out in the typewriter font like the rest of the snippet.
\verb and \verb*: Verbatim With Any Delimiter
\verb prints its text exactly as typed, spaces included, in the typewriter font. You choose the delimiter: the first character after \verb opens the snippet and the next copy of it closes the snippet. The LaTeX2e reference for \verb adds that the delimiter can't appear in the text and follows \verb directly, with no space.
Prefix the line with \verb|#!/usr/bin/env python3|.
In a shell pipe, \verb+ls | wc -l+ counts the files.
The starred form \verb*|a b| shows each space.
The first line prints the shebang untouched. The second uses + because the snippet contains a pipe. The third prints a␣b, with a visible space symbol where the space was.
\verb has two hard limits. The snippet can't contain a line break, or you get LaTeX Error: \verb ended by end of line. It also never breaks across lines, even at spaces, so long snippets stick out into the margin.
Watch the quotes, too. By default, an apostrophe in \verb|print('hi')| prints as a curly closing quote, which looks wrong in Python or shell code. Add \usepackage{upquote} and \verb prints straight quotes instead. The fix doesn't reach \texttt, though: when we compiled it, apostrophes there stayed curly.
Why Does \verb Fail Inside \section or \footnote?
\verb changes how LaTeX reads characters, but a command's argument has already been read before \verb gets a chance to run. By then the special characters are fixed, so the trick can't work. The reference manual says plainly that this isn't a fragility problem, so \protect won't help.
\section{Using \verb|abc|}
\footnote{Run \verb|x_1| first.}
The first line stops with LaTeX Error: \verb illegal in argument. The footnote stops with Missing $ inserted, because the underscore was already read as a math character. A \caption fails too, with a different error. You have three ways out, roughly in order of effort:
- Use
\textttwith escapes. Write\section{Using \texttt{x\_1}}. It needs no package and works in every argument. - Load fvextra and use
\Verb{...}. The fvextra manual says its\Verbworks in moving arguments like\sectionwhen you delimit it with braces. - Load cprotect. Prefix the outer command, as in
\cprotect\section{Using \verb|x_1|}or\cprotect\footnote{...}. The cprotect package passes the verbatim text through an auxiliary file.
None of these is free. \texttt makes you escape by hand. Inside another command, fvextra's \Verb can't contain # or %, and it collapses repeated spaces. cprotect's extra files add moving parts to every build. For a single heading, we'd take the \texttt route.
\lstinline: Inline Code in Your listings Style
\lstinline comes from the listings package. Its manual describes it as working like \verb while respecting the active language and style. If you already format code blocks with listings, set the style once and inline snippets will match.
\usepackage{listings}
\lstset{basicstyle=\ttfamily, language=Python}
Call \lstinline|print(len(items))| to check the count.
For one C snippet, \lstinline[language=C]+if (a | b)+ works too.
Both snippets print in the typewriter font, and keywords pick up whatever keywordstyle you set. The optional [language=C] changes the language for that one snippet only. As with \verb, you pick the delimiter, but to use [ you have to give an empty optional argument first.
The manual also offers a brace form, \lstinline{x = 1}, and marks it experimental. In our test, \lstinline{x = {1}} failed with Too many }'s, so keep braces out of brace-delimited snippets. \lstinline did survive inside a \section and a \footnote in our tests. The manual still calls that use experimental and asks you to put a backslash before any \, {, } or % in the snippet.
If you highlight code with minted instead, its \mintinline{python}{...} command fills the same role, using the Pygments library at compile time.
Long Inline Code That Runs Into the Margin
LaTeX's default typewriter fonts are declared with hyphenation switched off. A long path or URL in \texttt can't be split, so it pushes past the right margin and the log reports an Overfull \hbox.
% Before: one unbreakable chunk
The experiment reads all of its options from
\texttt{/home/researcher/projects/thesis/config/experiment\_settings.yaml}.
% After: three ways to allow breaks
The experiment reads all of its options from
\path{/home/researcher/projects/thesis/config/experiment_settings.yaml}.
\lstinline[breaklines=true]|/home/researcher/projects/thesis/config/run.yaml|
\texttt{/home/researcher/\allowbreak projects/\allowbreak thesis/}
Compiled, the \texttt version runs far past the right margin, while the \path and \lstinline versions fit with no warnings. Both still break only at allowed points, so a long path after a very short lead-in can overflow a little. \path comes from the url package, takes its argument verbatim and breaks after characters such as /, . and _.
\allowbreak gives you manual control inside \texttt. Put one after each slash where a break would read naturally. It's tedious for many paths, so we only use it when we can't add a package.
From Markdown Backticks to LaTeX \texttt
If you draft in Markdown, inline code written with backticks (covered in our Markdown code block guide) becomes \texttt on conversion. Pandoc's LaTeX writer does this, and our Pandoc walkthrough covers the commands. Our Markdown to LaTeX converter also emits \texttt and escapes the special characters for you, right in your browser.
Set `config_file ~/a^b` before you run it.
The converter turns that line into this LaTeX:
Set \texttt{config\_file \textasciitilde{}/a\textasciicircum{}b} before you run it.
It produces .tex source only, so you still compile the result in your own LaTeX setup.
LaTeX inline code comes down to one question: what's in the snippet, and where does it sit? Use \texttt with escapes for names and anything inside headings or footnotes, \verb for raw one-offs in body text, and \lstinline when listings already styles your blocks. Drafting in Markdown first? Write it in the editor, then run it through the Markdown to LaTeX converter to get escaped \texttt output.