Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to tell PDFLaTeX to make hyphenation in place where I want it to be?

I have my document file:

\documentclass[10pt,a4paper]{article}

\PassOptionsToPackage{hyphens}{url}\usepackage{hyperref}
\usepackage[margin=2.5cm]{geometry}

\begin{document}

This is text with \cite{reference}.

\bibliographystyle{unsrt}
\bibliography{bib_file}

\end{document}

and bib_file.bib file:

@misc{reference,
    title = {{Some dummy title in bib.}},
    howpublished = {\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html\#concurrency}},
    note = {Last access: 12.03.2013},
}

After building that file, the new line is places just after '.':

1st line: Some dummy title in bib. http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.
2nd line: html#concurrency. Last access: 12.03.2013.

But I wanted to insert hyphenation point on other place, like:

1st line: Some dummy title in bib. http://docs.oracle.com/javase/1.5.0/docs/
2nd line: relnotes/features.html#concurrency. Last access: 12.03.2013.

or even:

1st line: Some dummy title in bib. http://docs.oracle.com/javase/1.5.0/docs/ <-/
2nd line: relnotes/features.html#concurrency. Last access: 12.03.2013.

<-/ - is the enter arrow.

I tried to insert {\-} and \- into the url, but it adds in that place \discretionary{- }{}{} text

share|improve this question
    
Welcome to TeX.SE. It's not clear to me that there's actually a problem that needs to be solved -- at least for the MWE you've given us: Given a page size of A4 and margins of 2.5cm, it's perfectly sound to break the long URL after features., as the resulting line is neither overfull nor underfull (i.e., no big whitespaces). If you were to change the margins to 4cm, say, you'd get a line break after /docs/. –  Mico Mar 12 '13 at 18:48
1  
Breaking just before extension (like php, html, jsp, ...) is not good at all. I wanted to tell [PDF]LaTeX/BibTeX where the breaking the line in the url is (in my opinion) the best option. –  faramir Mar 12 '13 at 22:56

2 Answers 2

up vote 2 down vote accepted

Kurt's solution already shows how to fine tune the line breaking of URL's by specifying \UrlBreaks, \UrlBigBreaks, and \UrlNoBreaks.

To get a carriage return symbol at breaking points, we specify the breaking points as \UrlSpecials, that include the symbol when a line break occurs there.

\usepackage{dingbat}  % for \carriagereturn symbol
\newcommand{\urllb}{\discretionary{\carriagereturn}{}{}}
\renewcommand{\UrlBreaks}{}    % no standard breaking points
\renewcommand{\UrlBigBreaks}{}
\renewcommand{\UrlSpecials}{\do\.{\mathchar`\.\urllb}%
                            \do\/{\mathchar`\/\urllb}%
                            \do\@{\mathchar`\@\urllb}%
                            \do\\{\mathchar`\\\urllb}%
                            \do\-{\mathchar`\-\urllb}%
                            \do\#{\mathchar`\#\urllb}}% and so on

To specify breaking points by hand we use a symbol that hopefully won't occur in any URL like a star *, and make it a break URL's like the above symbols, but vanishing from the string:

\renewcommand{\UrlSpecials}{\do\*{\urllb}}

In order to have correct links with this solution the star needs to be filtered out from the link, too. We use the gorgeous solution of Qrrbrbirlbel:

\makeatletter
\def\strip@star#1*#2\@strip@star{%
    \expandafter\def\expandafter\strip@star@result\expandafter{\strip@star@result#1}%
    \if\relax\detokenize{#2}\relax\else
        \def\strip@star@temp{#2}%
        \expandafter\expandafter\expandafter\strip@star\expandafter\strip@star@temp
        \expandafter\@strip@star
    \fi
}
\patchcmd{\hyper@linkurl}{\Hy@pstringdef\Hy@pstringURI{#2}}{%
    \def\strip@star@result{}%
    \expandafter\strip@star#2*\@strip@star
    \Hy@pstringdef\Hy@pstringURI{\strip@star@result}%
}{}{}
\makeatother

Of course, both solutions can be mixed, i.e., we can specify the normal breaking points as \UrlSpecials, as well as the star for additional hand made breaking points.

Complete code:

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@misc{reference,
    title = {{Some dummy title in bib.}},
    howpublished = {\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#*concur*rency}},
    note = {Last access: 12.03.2013},
}
\end{filecontents*}
\documentclass[10pt,a4paper]{article}

\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}
\usepackage[margin=2.5cm]{geometry}

\usepackage{etoolbox}
\usepackage{dingbat}  % for \carriagereturn symbol
\newcommand{\urllb}{\discretionary{\carriagereturn}{}{}}
%% original breaking points of url package
% \def\UrlBreaks{\do\.\do\@\do\\\do\/\do\!\do\_\do\|\do\;\do\>\do\]%
%  \do\)\do\,\do\?\do\'\do+\do\=\do\#}%
% \def\UrlBigBreaks{\do\:\do@url@hyp}%

\makeatletter
\g@addto@macro{\UrlNoBreaks}{\do\.} % do not break after dot '.'
\makeatother

\renewcommand{\UrlBreaks}{}    % no standard breaking points
\renewcommand{\UrlBigBreaks}{}
\renewcommand{\UrlSpecials}{\do\_{\mathchar`\_\urllb}%
                            \do\/{\mathchar`\/\urllb}%
                            \do\@{\mathchar`\@\urllb}%
                            \do\\{\mathchar`\\\urllb}%
                            \do\-{\mathchar`\-\urllb}%
                            \do\#{\mathchar`\#\urllb}}% and so on
% \renewcommand{\UrlSpecials}{\do\*{\urllb}}

%% solution of Qrrbrbirlbel, see http://tex.stackexchange.com/q/103870/21591
\makeatletter
\def\strip@star#1*#2\@strip@star{%
    \expandafter\def\expandafter\strip@star@result\expandafter{\strip@star@result#1}%
    \if\relax\detokenize{#2}\relax\else
        \def\strip@star@temp{#2}%
        \expandafter\expandafter\expandafter\strip@star\expandafter\strip@star@temp
        \expandafter\@strip@star
    \fi
}
\patchcmd{\hyper@linkurl}{\Hy@pstringdef\Hy@pstringURI{#2}}{%
    \def\strip@star@result{}%
    \expandafter\strip@star#2*\@strip@star
    \Hy@pstringdef\Hy@pstringURI{\strip@star@result}%
}{}{}
\makeatother

\parindent0pt
\begin{document}
This is text with \cite{reference}.

Automatic breaking with carriage return symbol:

Text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

\bigskip

\renewcommand{\UrlSpecials}{\do\*{\urllb}}
Hand tuned URL breaking:

Text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

\bibliographystyle{unsrt}
\bibliography{faramir2}
\end{document}

yields

sample output

All hail to Qrrbrbirlbel for the hard part.

share|improve this answer

If you load package hyperref or package url LaTeX is able to find proper breaking points for an unmissleading hyphenation. As a result of this it often happen, that you get big white spaces in an justified text. It is not a good idea to insert in an url an additional - sign to mark a hyphenation. The reader does not know whether the - belongs to the url or is only a hyphenation sign. Urls are best hyphenated after a /, .etc.

I suggest to use package ragged2e and to use the command \raggedright to get a left justified bibliography. All the bibliographys I did this way looked very well!

Edit: I updated the MWE with a peace of code copied from this question and got the linebreak in the url in that way you want. But keep in mind: with this solution LaTeX decides were to break the url, not you with a special character.

Try this MWE:

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@misc{reference,
    title = {{Some dummy title in bib.}},
    howpublished = {\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html\#concurrency}},
    note = {Last access: 12.03.2013},
}
\end{filecontents*}

\documentclass[10pt,a4paper]{article}

\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}

%http://tex.stackexchange.com/questions/3033/forcing-linebreaks-in-url?rq=1
\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\g@addto@macro{\UrlNoBreaks}{\do\.} % do not break after dot '.'
\makeatother

\usepackage[%
  newcommands     % \RaggedRight=\raggedright etc. 
 ,newparameters   % use default settings of ragged2e
]{ragged2e}
\usepackage[margin=2.5cm]{geometry}

\begin{document}

This is text with \cite{reference}.

\bibliographystyle{unsrt}
{\raggedright  % group to end left justification after bib
\bibliography{\jobname}
}              % ends group for left justified bibliography

\end{document}
share|improve this answer
1  
When I run your MWE on the bib entry provided by the OP, I get a line break between the entry's title and the URL string (which now fits into the textblock without need for further linebreaks). This is probably not the desired outcome. If you replace \raggedright with \RaggedRight (the command provided by the ragged2e package that allows for some hyphenation), you'll get a linebreak after features., which is exactly the same result as if ragged2e wasn't loaded in the first place. –  Mico Mar 12 '13 at 19:01
1  
Simply loading ragged2e doesn't change the definition of \raggedright. You either need to use \RaggedRight or \usepackage[newcommands]{ragged2e} to get the redefined \raggedright if you want to benefit from ragged2e. –  cgnieder Mar 12 '13 at 20:17
    
@cgnieder yes you are right; I added now the options for ragged2e which I use for this purpose ... –  Kurt Mar 12 '13 at 20:25
    
Thank you but this don't resolve my problem - how to tell LaTeX where to break the url to make url best readable. What is more - I wanted to make an "enter arrow" (↵ - &#x21b5;) in place where the url is divided into two or more lines. –  faramir Mar 12 '13 at 23:00
1  
Thank you very much. I helps me to create: \makeatletter \g@addto@macro{\UrlNoBreaks}{\do\.} \makeatother so the break is not after dot and looks better (.). I don't know how to insert hyphenation character in the url (and make it like \hookleftarrow), but I think that is too hard for me now. –  faramir Mar 14 '13 at 19:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.