# How to format an inline source code

I have a document where I quiet often refer to source code element, such as class names, I wanted to format these strings differently, but at the beginning I didn't want to fiddle with various options I had, rather I defined my custom command as follows:

\providecommand{\inlinecode}[1]{\texttt{#1}}


Now my question is, what do you think is the best way to define \inlinecode? Note: I often use text with unescaped <, >, e.g., List<T>.

The \texttt looks fine to me, and works with <, >, but sometimes the words overflow the line. Is there any way to tell latex not to overflow the line, but rather "underflow" or insert more spaces between words, which seems to me as (maybe still ugly but) more acceptable than overflow? And is it possible to achieve it by only changing definition of my command, so I won't have to change the whole source?

• BTW: If you define a command like the one above you can also simply write \newcommand{\inlinecode}{\texttt} without passing the argument around. Even more efficient would be \let\inlinecode\texttt. May 25, 2011 at 9:43

use package listings instead. Then you can use \lstinline|<List>|. You can define the setting with \lstset{...}. A backgroundcolor is possible with a \colorbox:

\colorbox{blue}{\lstinline[basicstyle=\ttfamily\color{white}]|<List>|}

• \lstinline also accepts the settings as an optional argument, but for global settings \lstset{...} in the preamble is of course better. May 25, 2011 at 9:41
• Thank you for you answer. With \lstinline the text seems as normal, it is not different from the other text, which is not in \lstinline. Is it correct behaviour? I am looking for something that will change the font, so the inline code will look different than the other text. May 25, 2011 at 22:05
• backgroundcolor is not available for \lstinline. Jul 31, 2012 at 9:18
• @GarbageCollector: you can use a color box but, of course, with no linebreak
– user2478
Jan 28, 2013 at 18:59
• Herbert, I have the same question as Steves above. Could you give an example of how to make the format different from regular text? Jun 13, 2017 at 9:07

I wouldn't use \providecommand, to begin with: if you load a package that happens to define \inlinecode, chaos might happen.

The definition is good, although I would say

\newcommand{\inlinecode}{\texttt}


that avoids reading the argument twice, but this is a minor issue. Using a self-defined command is the right way, as you can freely redefine it in (almost) any way you like.

The main problem is, of course, that long words in typewriter type are a potential source for overfull lines. There's no automatic cure, apart from enclosing the paragraph in a sloppypar environment that sets the tolerance (a measure of how much you are accepting large spaces between words) to a very high value. You can also use the \sloppy declaration that sets those parameters "forever" (but respecting groups, of course).

What to do depends on the nature of the document: an informal report might be set with \sloppy, a formal book not.

My usual suggestion is to worry about bad line breaks only when the text is in its almost final form. Only at that time you can check the bad breaks one by one: it's often easy to find a way to rephrase the paragraph and avoid the bad break. When this fails, you can put \emergencypar at the end of the paragraph, where you have defined

\makeatletter
\newcommand{\emergencypar}[1][3em]{%
\begingroup\emergencystretch=#1\relax
\@@par\aftergroup\par\endgroup}
\makeatother


and try. You can also change the parameter by saying \emergencypar[4em] or other values. This will enlarge the interword spaces, but in a more uniform way than with \sloppy.

The command sets the \emergencystretch parameter inside a group, in which the primitive command \par (that LaTeX calls \@@par) is executed. We need to execute also \par with its present meaning, but outside the group, since this command is used by LaTeX for some internal workings.

Example (\lipsum* doesn't add \par at the end)

\documentclass{article}
\usepackage{lipsum}
\textwidth=5cm
\makeatletter
\newcommand{\emergencypar}[1][3em]{%
\begingroup\emergencystretch=#1\relax
\@@par\aftergroup\par\endgroup}
\makeatother

\begin{document}
\lipsum*[2]

\lipsum*[2]\emergencypar[2em]
\end{document}


Some Underfull \hbox messages are unavoidable, one might insert \hbadness=4000\relax after setting the \emergencystretch in the definition above to limit their appearance only to very bad lines.

• @Steves did you try it? How'd it go? Dec 29, 2016 at 3:38
• A good answer how to use \texttt along with \colorbox was given here tex.stackexchange.com/a/65292/131285 Apr 13, 2020 at 15:35

You can create your \inlinecode command which adapts to various languages.

Here is some piece of code I've been using and which was very helpful to me:

\usepackage{listings}
\usepackage{color}
\definecolor{lightgray}{gray}{0.9}

\lstset{
showstringspaces=false,
basicstyle=\ttfamily,
keywordstyle=\color{blue},
stringstyle=\color[RGB]{255,150,75}
}

\newcommand{\inlinecode}[2]{\colorbox{lightgray}{\lstinline[language=#1]$#2$}}


Then I use the command this way:

\inlinecode{Java}{import java.utils.LinkedList;}
\inlinecode{Java}{String s = "Hello, World!";}


and what I get is this:

Unfortunately I could not get it to compile with comments.

EDIT: You can change the font colors in \lstset and change/remove the background by modifying or removing \colorbox

• You have errors in your code! You define \newcommand{\inlinecode}[2]{\colorbox{lightgray}} and then place {\lstinline[language=#1]$#2$}} where the last brace is unbalanced! Oct 12, 2017 at 14:12
• @clemens Sorry for the little distraction, now it's fixed. Thank you for pointing out. Oct 12, 2017 at 14:21
• Doesn't work without \usepackage{color} Oct 29, 2017 at 19:03
• @winklerrr Answer fixed. I was working with a template which i didn't define, where the \usepackage{color} was already imported. Thanks. Oct 30, 2017 at 12:03
• While this works, it also fully breaks syntax highlighting in Texmaker and is therefore unusable for my purposes. Jul 2, 2019 at 13:05

A good way to solve this is with the tcolorbox package, and then add a custom tcbox to your preamble with the style declarations of choice:

\newtcbox{\inlinecode}{on line, boxrule=0pt, boxsep=0pt, top=2pt, left=2pt, bottom=2pt, right=2pt, colback=gray!15, colframe=white, fontupper={\ttfamily \footnotesize}}


This will result in a similar look to what is found on this site and others for inline code. As an example:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tcolorbox}
\newtcbox{\inlinecode}{on line, boxrule=0pt, boxsep=0pt, top=2pt, left=2pt, bottom=2pt, right=2pt, colback=gray!15, colframe=white, fontupper={\ttfamily \footnotesize}}
\begin{document}
This is a sample of some inline code: \inlinecode{int x = 0;}.
\end{document}


The output of this code looks like this:

• Simple, and great for referencing functions or other short snippets in-line. Jan 24, 2021 at 13:42
• can it be without the gray background? Jun 19 at 13:55
• @alper sure, just change the colback=gray!15 argument to whatever color you like. Jun 19 at 16:23