I would say
\newcommand{\x}[1]{%
{}$% get out of math
\kern-2\mathsurround % in case it's non zero
$% reenter math
\binoppenalty10000 \relpenalty10000 #1% typeset the subformula
{}$% get out of math
\kern-2\mathsurround % in case it's non zero
$% reenter math for the rest of the formula
}
TeX breaks formulas only after binary operators or relation symbols, the desirability of such breaks is measured by the two mentioned parameters. However the values used the penalties are those valid at the end of the formula, so simply enclosing #1
in \begingroup...\endgroup
and setting the values wouldn't do anything.
Of course this can work only if used in suitable places of the formula, for example $a+\x{b+c}$
would have the right spacing after the first +
(because of the empty subformula); the last empty subformula does nothing.
My opinion is still that bad breaks must be solved with suitably placed \nobreak
commands.
Some examples:
\documentclass[a4paper,draft]{book}
\newcommand{\x}[1]{{}$\kern-2\mathsurround${}
\binoppenalty10000 \relpenalty10000 #1{}$\kern-2\mathsurround${}}
\begin{document}
\parbox{5cm}{
A formula \(a+\x{c+d}\)\break showing that spaces are right
A new formula \(a+\x{c+d}\) showing that spaces are right
A brand new formula x \(a+\x{c+d}\) showing that spaces are right
A brand new formula xx \(a+\x{c+d}\) showing that spaces are right
A brand new formula xxx \(a+\x{c+d}\) showing that spaces are right
A brand new formula xxxx \(a+\x{c+d}\) showing that spaces are right
Another brand new formula \(a+\x{c+d}\) showing that spaces are right
Right: $\sin(\x{a+b})$
Wrong: $\sin\x{(a+b)}$
\mathsurround=30pt
A formula xxxxxxx \(a+\x{c+d}\) showing mathsurround
A formula xxxxxxx \(a+c+d\) showing mathsurround
}
\end{document}
Addition about usage
The \x
macro (possibly with a more descriptive name) should be used in specific places. Its contents must
(1) start with an ordinary symbol or be preceded by an ordinary symbol;
(2) end with an ordinary symbol or be followed by one.
It doesn't support the style declarations \displaystyle
, \textstyle
, \scriptstyle
, or \scriptscriptstyle
; it may make sense to carry a \displaystyle
declaration, this might be done with a *-variant.
It doesn't support \left
or \right
: it's not allowed something like
$...\left(\x{a+b}\right)...$
but this is not a problem, as no formula can be split at relation or operation symbols between \left
and \right
and the spaces around these symbols never participates to stretching or shrinking.
Do you need to use breakurl
? If not, \usepackage[hyphens]{url}
mostly works (the box around the hypenated URL is not quite right).
\documentclass{article}
\usepackage[hyphens]{url}
\usepackage{hyperref}
\begin{document}
\url{https://tex.stackexchange.com/questions/28835/option-to-break-urls-with-carriage-return-symbol}
\bigskip% An alternative, if this is meant for online use:
\href{https://tex.stackexchange.com/questions/28835/option-to-break-urls-with-carriage-return-symbol}{Question about to break in URLs}
\end{document}
The [hyphens]
option also seems to solve the problem with the bibliography (adapted form Url references in bibliography):
\documentclass{article}
\usepackage[hyphens]{url}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@misc{A01,
author = {Lerner, Ben},
year = {2011},
title = {Break URLs},
url = {http://short.domain.com/very-long-dashed-section},
}
\end{filecontents}
\begin{document}
\nocite{*}
\bibliographystyle{alphaurl}
\bibliography{\jobname}
\end{document}
UPDATE: 2011-09-20:
Borrowing from Replace hyphenation character by a backwards arrow, this uses a \hookleftarrow
to be hyphen separator and seems to work. Here I have allowed every character to be breakable, but if that is not desired, you could selectively insert the \BreakableChar
only at the characters you want to be breakable:
\documentclass{article}
\usepackage{hyphenat}
\usepackage{url}
\usepackage{xstring}
\usepackage{forloop}
\usepackage{xcolor}
%\usepackage{filecontents}
\newsavebox\MyArrowBox%
\sbox\MyArrowBox{$\hookleftarrow$}%
\makeatletter%
\newcommand*{\BreakableChar}{%
\leavevmode%
\prw@zbreak%
\discretionary{\usebox\MyArrowBox}{}{}%
\prw@zbreak%
}%
\newcounter{index}%
\newcommand{\AddBreakableChars}[1]{%
\StrLen{#1 }[\stringLength]%
\forloop[1]{index}{1}{\value{index}<\stringLength}{%
\StrChar{#1}{\value{index}}[\currentLetter]%
{\currentLetter\BreakableChar}%
}%
}%
\newcommand*{\MyUrl}{https://tex.stackexchange.com/questions/28835/option-to-break-urls-with-carriage-return-symbol}%
\begin{document}
\parbox{4cm}{\textcolor{blue}{\AddBreakableChars{\MyUrl}}}
\bigskip
\parbox{7cm}{\textcolor{blue}{\AddBreakableChars{\MyUrl}}}
\end{document}
Best Answer
You could insert
\allowbreak
whereever a break without hyphen shall be allowed, such as