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}
Manual Solution:
You could also do it manually, by inserting a \BreakableChar
at the appropriate points:
Thisisaverylongtexttosimulateerror “firstterm*[title/abstract] AND
(secondterm*\BreakableChar{}[title/abstract] OR thirdterm*[title/abstract])”
One small complication here is that I have defined \BreakableChar
with an optional parameter, and since your subsequent text begins with a [
, it necessitated the use of {}
.
Automated Solution:
You could adapt the solution from Automatic line breaking of long lines of text
too automatically insert a breakable char at the points where you want it. To use this just pass the entire string to \AddBreakableChars{}
:
Thisisaverylongtexttosimulateerror “firstterm*[title/abstract] AND
\AddBreakableChars{(secondterm*[title/abstract]} OR thirdterm*[title/abstract])”
The current definition of it will insert a \BreakableChar
following any *
or \
. This can be extended to apply to additional characters as well. I have also shown in commented code what to do for cases where there is a character where you want to break and do want to insert a hyphen.
Results:
Here is the original text followed by the text where the term is given to the automated, \AddBreakableChars{}
macro, and the one with a \BreakableChar
manually inserted at the appropriate point:
Code:
\documentclass{article}
\usepackage{showframe}
\usepackage{hyphenat}
\usepackage{xstring}
\usepackage{forloop}
\newsavebox\MyBreakChar%
\sbox\MyBreakChar{}% char to display the break after non char
\newsavebox\MySpaceBreakChar%
\sbox\MySpaceBreakChar{\hyp}% char to display the break after space
\makeatletter%
\newcommand*{\BreakableChar}[1][\MyBreakChar]{%
\leavevmode%
\prw@zbreak%
\discretionary{\usebox#1}{}{}%
\prw@zbreak%
}%
\makeatother
\newcounter{index}%
\newcommand{\AddBreakableChars}[1]{%
\StrLen{#1 }[\stringLength]%
\forloop[1]{index}{1}{\value{index}<\stringLength}{%
\StrChar{#1}{\value{index}}[\currentLetter]%
\IfStrEqCase{\currentLetter}{%
% All the characters where you don't want hypen
{*}{\currentLetter\BreakableChar[\MyBreakChar]}%
{/}{\currentLetter\BreakableChar[\MyBreakChar]}%
% All the charactes where a break should have a hypen
%{ }{\currentLetter\BreakableChar[\MySpaceBreakChar]}%
}[\currentLetter]%
}%
}%
\begin{document}
\noindent\textbf{Original Text}:
\par\noindent
Thisisaverylongtexttosimulateerror “firstterm*[title/abstract] AND
(secondterm*[title/abstract] OR thirdterm*[title/abstract])”
\medskip
\noindent\textbf{Use automated} \verb|\AddBreakableChars|:
\par\noindent
Thisisaverylongtexttosimulateerror “firstterm*[title/abstract] AND
\AddBreakableChars{(secondterm*[title/abstract]} OR thirdterm*[title/abstract])”
\medskip
\noindent\textbf{Use manual} \verb|\BreakableChar|:
\par\noindent
Thisisaverylongtexttosimulateerror “firstterm*[title/abstract] AND
(secondterm*\BreakableChar{}[title/abstract] OR thirdterm*[title/abstract])”
\end{document}
Best Answer
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 deletebrk
if that is desired.