\verb
it continues past the margin. Is there a way to tell LaTeX to place the whole thing on the next line if it doesn't fit on this one?Text Formatting ⇒ Is there a way to treat \verb as unbreakable word
-
- Posts: 3
- Joined: Sun Jun 23, 2013 2:59 pm
Is there a way to treat \verb as unbreakable word
I have some code that I want to add to my text as an inline verbatim, but, using
Last edited by cgnieder on Sun Jun 23, 2013 4:00 pm, edited 1 time in total.
NEW: TikZ book now 40% off at Amazon.com for a short time.

Is there a way to treat \verb as unbreakable word
Hi Zoefschildpad,
welcome to the LaTeX community!
You can add
Regards
welcome to the LaTeX community!
You can add
\linebreak
before the \verb
expression but that could cause too much inter-word spacing. It's probably simpler to rephrase the sentence. But maybe this should wait until the document is finished. If you decide to change the text the problem might go away by itself.Regards
site moderator & package author
-
- Posts: 3
- Joined: Sun Jun 23, 2013 2:59 pm
Re: Is there a way to treat \verb as unbreakable word
Yeah I assumed I could do it manually, I was just wondering if there was a way to automate it.
Doing this kind of thing by hand is just asking for trouble in the next version...
Doing this kind of thing by hand is just asking for trouble in the next version...
Is there a way to treat \verb as unbreakable word
Well, exactly how do you want to automate it? You can't make it always break the line since that will be inappropriate when
Regards
\verb
is in the middle of the line. So you'd need something like \linebreak[3]
in front of it (which could be done) to encourage but not enforce a line break. But depending on the resulting badness of the stretched inter-word space the line break may not occur and you'd have gained nothing. The alternative would be to allow the verbatim expression to break across lines.Regards
site moderator & package author
-
- Posts: 3
- Joined: Sun Jun 23, 2013 2:59 pm
Re: Is there a way to treat \verb as unbreakable word
I am very far from an expert on LaTeX, but here's what I had in mind.
LaTeX would build the page up to the beginning of the \verb (or whatever it would need to be replaced with), then it would build the verb block, compares it's dimensions with how much space is left on the line and then place it there or on the next line as fits the margins, in much the same way it would put the word "speed" on a new line if there were only room for 2 characters. In this scenario the only way it would exceed the margins is if I made the verb block too long to fit on a full line.
It clearly doesn't do that so I was asking for a way to change that behavior. If you're saying that's impossible (which seems to be the case) than that's disappointing and I will do it manually.
LaTeX would build the page up to the beginning of the \verb (or whatever it would need to be replaced with), then it would build the verb block, compares it's dimensions with how much space is left on the line and then place it there or on the next line as fits the margins, in much the same way it would put the word "speed" on a new line if there were only room for 2 characters. In this scenario the only way it would exceed the margins is if I made the verb block too long to fit on a full line.
It clearly doesn't do that so I was asking for a way to change that behavior. If you're saying that's impossible (which seems to be the case) than that's disappointing and I will do it manually.
Is there a way to treat \verb as unbreakable word
Well, in a way this is what TeX already does but it considers all lines of a paragraph, all resulting overfullZoefschildpad wrote:LaTeX would build the page up to the beginning of the \verb (or whatever it would need to be replaced with), then it would build the verb block, compares it's dimensions with how much space is left on the line and then place it there or on the next line as fits the margins, in much the same way it would put the word "speed" on a new line if there were only room for 2 characters. In this scenario the only way it would exceed the margins is if I made the verb block too long to fit on a full line.
\hbox
s, the stretch or shrink of the inter-word spacing and a few more parameters and calculates a badness for the resulting paragraph. It then typesets the paragraph that results in the least badness. One can change the parameters that make TeX decide (in this case \tolerance
and \emergencystretch
come to mind, also LaTeX's \sloppy
) but that would either affect the whole document (and maybe result in a lot of badly typeset paragraphs for the gain of a few overfull boxes less) or one has to do it locally which again would be a manual decision on a case by case choice. A patch that would work only for lines where \verb
is used is impossible.Indeed the recommended approach to deal with ugly overfull boxes is to wait until the document is finished, then try to get rid of them by rephrasing and manually tweaking with local settings of above-mentioned parameters.
Code: Select all
\documentclass{article}
\usepackage{showframe}
\begin{document}
{this is a long line which is just long enough to cause the following
\verb+\verb!expression!+ to hang into the margin.}
{\setlength\emergencystretch{2em}%
this is a long line which is just long enough to cause the following
\verb+\verb!expression!+ to hang into the margin.\par}
this is a long line which is just long enough to cause the following\linebreak
\verb+\verb!expression!+ to hang into the margin.
{\tolerance=1000
this is a long line which is just long enough to cause the following
\verb+\verb!expression!+ to hang into the margin.\par}
{\sloppy
this is a long line which is just long enough to cause the following
\verb+\verb!expression!+ to hang into the margin.\par}
this is a long line which is just long enough to cause the following
\verb+\verb!expression!+ to hang into the margin.
\end{document}
site moderator & package author