# Line not breaking when combining inline math and \texttt

Sadly, I am not able to recreate a MWE since I'm working on a fairly complex template. I cannot understand how LaTeX decides to break lines, I googled but I couldn't find a clear cut answer. This is the code I'm working with:

\begin{itemize}

% [...]

\item A TOSCA requirement definition within a node type definition was encoded with the term
\texttt{requirement(}$RName$\texttt{, }$RCap$\texttt{, }$RNType$\texttt{, }$RRel$\texttt{, occurrences(}$ROccLB$\texttt{, }$ROccUB$\texttt{))}, where $RName$ is an atom for the name of the
requirement, $RCap$ is an atom for the required capability, $RNType$ is an atom for the type of nodes admitted
as a target (including subtypes), $RRel$ is an atom representing the relationship underlying the requirement,
and $ROccLB$ and $ROccUB$ are respectively lower and upper bound for the number of occurrences of the
requirement, $ROccUB$ possibly being the atom \texttt{unbounded};

% [...]

\end{itemize}


The item is typeset like this:

If I add a newline (a literal newline, not a \\, followed by indentation) after the first occurrence of $RRel$ in the code, the line is broken correctly after $ROccLB$, like so:

I get it that LaTeX works in mysterious ways, but I would like to understand this behaviour.

MWE

\documentclass{article}

\addtolength\textwidth{45pt}
\begin{document}

\begin{itemize}

\item A TOSCA requirement definition within a node type definition was
encoded with the term \texttt{requirement(}$RName$\texttt{,
}$RCap$\texttt{, }$RNType$\texttt{, }$RRel$\texttt{,
occurrences(}$ROccLB$\texttt{, }$ROccUB$\texttt{))}, where $RName$
is an atom for the name of the requirement, $RCap$ is an atom for
the required capability, $RNType$ is an atom for the type of nodes
admitted as a target (including subtypes), $RRel$ is an atom
representing the relationship underlying the requirement, and
$ROccLB$ and $ROccUB$ are respectively lower and upper bound for the
number of occurrences of the requirement, $ROccUB$ possibly being
the atom \texttt{unbounded};

\item A TOSCA requirement definition within a node type definition was
encoded with the term \texttt{requirement(}$RName$\texttt{,
}$RCap$\texttt{, }$RNType$\texttt{, }$RRel$ \texttt{,
occurrences(}$ROccLB$\texttt{, }$ROccUB$\texttt{))}, where $RName$
is an atom for the name of the requirement, $RCap$ is an atom for
the required capability, $RNType$ is an atom for the type of nodes
admitted as a target (including subtypes), $RRel$ is an atom
representing the relationship underlying the requirement, and
$ROccLB$ and $ROccUB$ are respectively lower and upper bound for the
number of occurrences of the requirement, $ROccUB$ possibly being
the atom \texttt{unbounded};

\end{itemize}
\end{document}

• it looks very odd that you use math for this. And why is the comma in typewriter?? Feb 1 at 12:08
• in math \texttt is like \mbox and an unbreakable box. I would have used \mathtt and only around each identifier separately not around the ( or , (italic names like RNmae should be in \mathit not the default math font which is designed to make adjacent letters look like a product of 1-letter variables not a word. the newline (a space would be the same) is adding an inter-word space so an allowed line breaking point. Feb 1 at 12:19
• You can always provide a MWE the complexity of the original document is not releavant the only thing that affects the linebreaking here is the font and the text width. I added a document showing the original overfull line and the version with the extra space added where you suggested. Feb 1 at 12:30
• Thanks for the MWE, I didn't know what to look for. Feb 1 at 12:42

## 2 Answers

I wouldn't use math mode. instead, I'd use text mode thoughout, for both function names (e.g., \texttt) and variable names (e.g., \textit). And, if and where necessary, just insert \- discretionary hyphens.

\documentclass{article} % or some other suitable document class
\newcommand\fn[1]{\texttt{#1}} % function name
\newcommand\vn[1]{\textit{#1}} % variable name

\begin{document}
\begin{itemize}
\item A TOSCA requirement definition within a node type definition
was encoded with the term \fn{requirement}[\vn{RName}, \vn{RCap},
\vn{RNType}, \vn{RRel}, \fn{occur\-rences}(\hspace{0pt}\vn{ROccLB},
\vn{ROccUB})], where \vn{RName} is an atom for the name of the
requirement, \vn{RCap} is an atom for the required capability,
\vn{RNType} is an atom for the type of nodes admitted as a target
(including subtypes), \vn{RRel} is an atom representing the relationship
underlying the requirement, and \vn{ROccLB} and \vn{ROccUB} are
respectively lower and upper bound for the number of occurrences
of the requirement, \vn{ROccUB} possibly being the atom
\fn{unbounded}; \dots
\end{itemize}
\end{document}


If you use \mathit so the text is a bit mmore compressed (and readable) and allow the space after a comma in math some flexibility then it (almost) fits on one line without breaking, see thethird item below.

\documentclass{article}

\addtolength\textwidth{45pt}
\begin{document}

\begin{itemize}

\item A TOSCA requirement definition within a node type definition was
encoded with the term \texttt{requirement(}$RName$\texttt{,
}$RCap$\texttt{, }$RNType$\texttt{, }$RRel$\texttt{,
occurrences(}$ROccLB$\texttt{, }$ROccUB$\texttt{))}, where $RName$
is an atom for the name of the requirement, $RCap$ is an atom for
the required capability, $RNType$ is an atom for the type of nodes
admitted as a target (including subtypes), $RRel$ is an atom
representing the relationship underlying the requirement, and
$ROccLB$ and $ROccUB$ are respectively lower and upper bound for the
number of occurrences of the requirement, $ROccUB$ possibly being
the atom \texttt{unbounded};

\item A TOSCA requirement definition within a node type definition was
encoded with the term \texttt{requirement(}$RName$\texttt{,
}$RCap$\texttt{, }$RNType$\texttt{, }$RRel$ \texttt{,
occurrences(}$ROccLB$\texttt{, }$ROccUB$\texttt{))}, where $RName$
is an atom for the name of the requirement, $RCap$ is an atom for
the required capability, $RNType$ is an atom for the type of nodes
admitted as a target (including subtypes), $RRel$ is an atom
representing the relationship underlying the requirement, and
$ROccLB$ and $ROccUB$ are respectively lower and upper bound for the
number of occurrences of the requirement, $ROccUB$ possibly being
the atom \texttt{unbounded};

\item \sloppy \thinmuskip=3mu plus 2mu minus 1.5mu A TOSCA requirement definition within a node type definition was
encoded with the term $\mathtt{requirement}(\mathit{RName}, \mathit{RCap}, \mathit{RNType}, \mathit{RRel}, \mathtt{occurrences}(\mathit{ROccLB}, \mathit{ROccUB}))$, where $\mathit{RName}$
is an atom for the name of the requirement, $\mathit{RCap}$ is an atom for
the required capability, $\mathit{RNType}$ is an atom for the type of nodes
admitted as a target (including subtypes), $\mathit{RRel}$ is an atom
representing the relationship underlying the requirement, and
$\mathit{ROccLB}$ and $\mathit{ROccUB}$ are respectively lower and upper bound for the
number of occurrences of the requirement, $\mathit{ROccUB}$ possibly being
the atom \texttt{unbounded};

\end{itemize}
\end{document}