I would like to have math mode expressions in dollar signs always to stay on the same line, which is what happens to a sequence of characters with no spaces while not in math mode. Is there a way to avoid having to do this manually by adding line breaks every time (which is terrible style, and gets very messy if one has to go back and change things, altering everithing that comes after)?
[Tex/LaTex] Forcing math mode to be on the same line
line-breakingmath-mode
Related Solutions
This works for T1 and OT1 encodings, would need modification for other encodings (basically needs some invisible character to use as a fake hyphenation character)
As can be seen if no break is added (first example) no arrow, or arrows will be added if it breaks after the URL syntax /
or .
(second example) or at a hyphenation point such as exam-ple (third example)
As posted the arrows stick into the right margin, if you prefer them to be within the text block remove the \rlap
from the \discretionary
.
Also as posted this defines \brkurl
the question asks for the command to be called \url
just globally delete brk
if that is desired.
\documentclass{article}
%\tracingonline1
%\showboxbreadth=200
%\showboxdepth=200
\begin{document}
\def\addurlspace#1{%
\ifx\relax#1%
\else
\ifx/#1\space\fi
\ifx.#1\space\fi
#1%
\ifx/#1\space\fi
\ifx.#1\space\fi
\expandafter\addurlspace
\fi}
\makeatletter
\@namedef{OT1-zwidthchar}{255}
\@namedef{T1-zwidthchar}{"17}
\def\brkurl#1{%
\edef\savedhchar{\the\hyphenchar\font}%
\global\setbox1\hbox{}%
\setbox0=\vbox{\hsize=2pt\rightskip=0pt plus 1fill
\hfuzz\maxdimen
\tracinglostchars0
\overfullrule0pt
\hyphenchar\font=\csname \f@encoding-zwidthchar\endcsname
\noindent \hskip0pt \addurlspace #1\relax
\par
\loop
\setbox4 \lastbox
\ifvoid4 \else
\global\setbox1\hbox{\unhbox4\unskip\unskip\discretionary{\hbox{\rlap{$\leftarrow$}}}{}{}\unhbox1}%
\unskip
\unskip
\unpenalty
\unskip
\repeat
}%
\unhbox1
\hyphenchar\font\savedhchar
\relax}
\makeatother
some text \brkurl{http://www.example.com/this/directory/here}
some text \brkurl{http://www.example.com/this/directory/here} some text
some text \brkurl{http://www.example.com/this/directory/here} some text
\end{document}
This is the consequence of a bad decision in unicode-math
: they named \mathit
the normal math italic letters, instead of respecting the LaTeX convention of referring to them as \mathnormal
; \mathit
should choose the text italic font, in order to make unicode-math
a drop-in replacement.
I wouldn't expect different output when unicode-math
is loaded or not; but this simple example shows the bug:
\documentclass{article}
%\usepackage{unicode-math}
\begin{document}
$\mathit{different}$
$different$
\end{document}
If the line with unicode-math
is commented out, we get
If I uncomment the line, I get
which is definitely wrong.
Workaround:
Define a new math alphabet:
\documentclass{article}
\usepackage{unicode-math}
\DeclareMathAlphabet{\Lmathit}{\encodingdefault}{\familydefault}{m}{it}
\begin{document}
$\Lmathit{different}$
$different$
\end{document}
If you use lhs2TeX
, you can add
\renewcommand{\Conid}[1]{\Lmathit{#1}}
\renewcommand{\Varid}[1]{\Lmathit{#1}}
after loading it.
This shouldn't raise the Too many math alphabets
error; if it does, then add the code you find between \makeatletter
and \makeatother
in https://tex.stackexchange.com/a/100428/4427
Best Answer
The following two lines put at the document preamble should help:
It is a good idea to add
\begin{sloppy}...\end{sloppy}
to the paragraphs where mathematics appear, so that you allow the lines to be more stretchy. You can allow this stretch globally by putting\sloppy
into the preamble.However, this will completely forbid implicit linebreaks inside inline mathematics. I don't think it is a good idea. Better idea would be to set the pelanties to some value that will discourage the breaks, but allow them if really necessary: