Skip to main content
Fixing a site bug of disappearing double backslashes: https://tex.meta.stackexchange.com/questions/7168/double-backslashes-disappear-from-code
Source Link
David Carlisle
  • 828.7k
  • 74
  • 1.7k
  • 2.6k

This solution checks if the url is longer than the line width. If it is, then it inserts \\ (since there is no way it can fit on a line) and then typesets the url. If it isn't larger, then it will fit on a line, so we typeset it as a box with a stretchable space before it. If the box does not fit on the line, then the space will stretch to fill the rest of the line so the line won't be underfull and the url will appear on the next line.

\documentclass[twocolumn]{article} \usepackage{lipsum} \usepackage{url} \let\oldurl\url \makeatletter \renewcommand*\url{% \begingroup \let\do\@makeother \dospecials \catcode`{1 \catcode`}2 \catcode`\ 10 \url@aux } \newcommand*\url@aux[1]{% \setbox0\hbox{\oldurl{#1}}% \ifdim\wd0>\linewidth \strut \ \\  \vbox{% \hsize=\linewidth \kern-\lineskip \raggedright \strut\oldurl{#1}% }% \else \hskip0pt plus\linewidth \penalty0 \box0 \fi \endgroup } \makeatother \begin{document} \lipsum[1] Here is some text \url{http://www.example.com/~example/foo/bar/baz/index%20here.html} Here is some text \url{http://example.org/index.html} Short \url{http://example.org} \end{document} 

Edit:
I modified it to try to prevent the underfull hbox as well as deal with specials in the argument better than just using \detokenize.

This solution checks if the url is longer than the line width. If it is, then it inserts \\ (since there is no way it can fit on a line) and then typesets the url. If it isn't larger, then it will fit on a line, so we typeset it as a box with a stretchable space before it. If the box does not fit on the line, then the space will stretch to fill the rest of the line so the line won't be underfull and the url will appear on the next line.

\documentclass[twocolumn]{article} \usepackage{lipsum} \usepackage{url} \let\oldurl\url \makeatletter \renewcommand*\url{% \begingroup \let\do\@makeother \dospecials \catcode`{1 \catcode`}2 \catcode`\ 10 \url@aux } \newcommand*\url@aux[1]{% \setbox0\hbox{\oldurl{#1}}% \ifdim\wd0>\linewidth \strut \  \vbox{% \hsize=\linewidth \kern-\lineskip \raggedright \strut\oldurl{#1}% }% \else \hskip0pt plus\linewidth \penalty0 \box0 \fi \endgroup } \makeatother \begin{document} \lipsum[1] Here is some text \url{http://www.example.com/~example/foo/bar/baz/index%20here.html} Here is some text \url{http://example.org/index.html} Short \url{http://example.org} \end{document} 

Edit:
I modified it to try to prevent the underfull hbox as well as deal with specials in the argument better than just using \detokenize.

This solution checks if the url is longer than the line width. If it is, then it inserts \\ (since there is no way it can fit on a line) and then typesets the url. If it isn't larger, then it will fit on a line, so we typeset it as a box with a stretchable space before it. If the box does not fit on the line, then the space will stretch to fill the rest of the line so the line won't be underfull and the url will appear on the next line.

\documentclass[twocolumn]{article} \usepackage{lipsum} \usepackage{url} \let\oldurl\url \makeatletter \renewcommand*\url{% \begingroup \let\do\@makeother \dospecials \catcode`{1 \catcode`}2 \catcode`\ 10 \url@aux } \newcommand*\url@aux[1]{% \setbox0\hbox{\oldurl{#1}}% \ifdim\wd0>\linewidth \strut \\  \vbox{% \hsize=\linewidth \kern-\lineskip \raggedright \strut\oldurl{#1}% }% \else \hskip0pt plus\linewidth \penalty0 \box0 \fi \endgroup } \makeatother \begin{document} \lipsum[1] Here is some text \url{http://www.example.com/~example/foo/bar/baz/index%20here.html} Here is some text \url{http://example.org/index.html} Short \url{http://example.org} \end{document} 

Edit:
I modified it to try to prevent the underfull hbox as well as deal with specials in the argument better than just using \detokenize.

Fixed
Source Link
TH.
  • 65.1k
  • 17
  • 169
  • 225

This solution checks if the url is longer than the line width. If it is, then it inserts \\ (since there is no way it can fit on a line) and then typesets the url. If it isn't larger, then it will fit on a line, so we typeset it as a box with a stretchable space before it. If the box does not fit on the line, then the space will stretch to fill the rest of the line so the line won't be underfull and the url will appear on the next line.

\documentclass[twocolumn]{article} \usepackage{lipsum} \usepackage{url} \let\oldurl\url \makeatletter \renewcommand*\url{% \begingroup \let\do\@makeother \dospecials \catcode`{1 \catcode`}2 \catcode`\ 10 \url@aux } \newcommand*\url@aux[1]{% \setbox0\hbox{\oldurl{#1}}% \ifdim\wd0>\linewidth \strut \ \vbox{% \parindent=0pt\hsize=\linewidth \kern-\lineskip \raggedright \strut\oldurl{#1}% }% \else \hskip0pt plus\linewidth \penalty0 \box0 \fi \endgroup } \makeatother \begin{document} \lipsum[1] Here is some text \url{http://www.example.com/~example/foo/bar/baz/index%20here.html} Here is some text \url{http://example.org/index.html} Short \url{http://example.org} \end{document} 

Edit:
I modified it to try to prevent the underfull hbox as well as deal with specials in the argument better than just using \detokenize.

This solution checks if the url is longer than the line width. If it is, then it inserts \\ (since there is no way it can fit on a line) and then typesets the url. If it isn't larger, then it will fit on a line, so we typeset it as a box with a stretchable space before it. If the box does not fit on the line, then the space will stretch to fill the rest of the line so the line won't be underfull and the url will appear on the next line.

\documentclass[twocolumn]{article} \usepackage{lipsum} \usepackage{url} \let\oldurl\url \makeatletter \renewcommand*\url{% \begingroup \let\do\@makeother \dospecials \catcode`{1 \catcode`}2 \catcode`\ 10 \url@aux } \newcommand*\url@aux[1]{% \setbox0\hbox{\oldurl{#1}}% \ifdim\wd0>\linewidth \strut \ \vbox{% \parindent=0pt \kern-\lineskip \raggedright \strut\oldurl{#1}% }% \else \hskip0pt plus\linewidth \penalty0 \box0 \fi \endgroup } \makeatother \begin{document} \lipsum[1] Here is some text \url{http://www.example.com/~example/foo/bar/baz/index%20here.html} Here is some text \url{http://example.org/index.html} Short \url{http://example.org} \end{document} 

Edit:
I modified it to try to prevent the underfull hbox as well as deal with specials in the argument better than just using \detokenize.

This solution checks if the url is longer than the line width. If it is, then it inserts \\ (since there is no way it can fit on a line) and then typesets the url. If it isn't larger, then it will fit on a line, so we typeset it as a box with a stretchable space before it. If the box does not fit on the line, then the space will stretch to fill the rest of the line so the line won't be underfull and the url will appear on the next line.

\documentclass[twocolumn]{article} \usepackage{lipsum} \usepackage{url} \let\oldurl\url \makeatletter \renewcommand*\url{% \begingroup \let\do\@makeother \dospecials \catcode`{1 \catcode`}2 \catcode`\ 10 \url@aux } \newcommand*\url@aux[1]{% \setbox0\hbox{\oldurl{#1}}% \ifdim\wd0>\linewidth \strut \ \vbox{% \hsize=\linewidth \kern-\lineskip \raggedright \strut\oldurl{#1}% }% \else \hskip0pt plus\linewidth \penalty0 \box0 \fi \endgroup } \makeatother \begin{document} \lipsum[1] Here is some text \url{http://www.example.com/~example/foo/bar/baz/index%20here.html} Here is some text \url{http://example.org/index.html} Short \url{http://example.org} \end{document} 

Edit:
I modified it to try to prevent the underfull hbox as well as deal with specials in the argument better than just using \detokenize.

better
Source Link
TH.
  • 65.1k
  • 17
  • 169
  • 225

This solution checks if the url is longer than the line width. If it is, then it inserts \\ (since there is no way it can fit on a line) and then typesets the url. If it isn't larger, then it will fit on a line, so we typeset it as a box with a stretchable space before it. If the box does not fit on the line, then the space will stretch to fill the rest of the line so the line won't be underfull and the url will appear on the next line.

\documentclass[twocolumn]{article} \usepackage{lipsum} \usepackage{url} \newcommand*\makeurl[1] \let\oldurl\url \makeatletter \renewcommand*\url{% \begingroup \expandafter\Url\expandafter{\detokenize\let\do\@makeother \dospecials \catcode`{#1}}%1  \catcode`}2 \renewcommand*\url[1]{% \catcode`\ 10 \begingroup\url@aux } \newcommand*\url@aux[1]{% \setbox0\hbox{\makeurl\oldurl{#1}}% \ifdim\wd0>\linewidth   \strut  \ \makeurl\vbox{% \parindent=0pt \kern-\lineskip \raggedright \strut\oldurl{#1}% }% \else \hskip0pt plus\linewidth \penalty0 \box0 \fi \endgroup } \makeatother \begin{document} \lipsum[1] Here is some text \url{http://www.example.com/~example/foo/bar/baz/indexindex%20here.html} Here is some text \url{http://example.org/index.html} Short \url{http://example.org} \end{document} 

This doesn't quite work for hyperref, butEdit:
I modified it is easy to modify ittry to work. Just save \url with \let\oldurl\url and then omitprevent the \begingroup and change \Url to \oldurlunderfull hbox as well as deal with specials in the argument better than just using \makeurl\detokenize.

This solution checks if the url is longer than the line width. If it is, then it inserts \\ (since there is no way it can fit on a line) and then typesets the url. If it isn't larger, then it will fit on a line, so we typeset it as a box with a stretchable space before it. If the box does not fit on the line, then the space will stretch to fill the rest of the line so the line won't be underfull and the url will appear on the next line.

\documentclass[twocolumn]{article} \usepackage{lipsum} \usepackage{url} \newcommand*\makeurl[1]{% \begingroup \expandafter\Url\expandafter{\detokenize{#1}}% } \renewcommand*\url[1]{% \begingroup \setbox0\hbox{\makeurl{#1}}% \ifdim\wd0>\linewidth \ \makeurl{#1}% \else \hskip0pt plus\linewidth \penalty0 \box0 \fi \endgroup } \begin{document} \lipsum[1] Here is some text \url{http://www.example.com/~example/foo/bar/baz/index.html} Here is some text \url{http://example.org/index.html} Short \url{http://example.org} \end{document} 

This doesn't quite work for hyperref, but it is easy to modify it to work. Just save \url with \let\oldurl\url and then omit the \begingroup and change \Url to \oldurl in \makeurl.

This solution checks if the url is longer than the line width. If it is, then it inserts \\ (since there is no way it can fit on a line) and then typesets the url. If it isn't larger, then it will fit on a line, so we typeset it as a box with a stretchable space before it. If the box does not fit on the line, then the space will stretch to fill the rest of the line so the line won't be underfull and the url will appear on the next line.

\documentclass[twocolumn]{article} \usepackage{lipsum} \usepackage{url}  \let\oldurl\url \makeatletter \renewcommand*\url{% \begingroup \let\do\@makeother \dospecials \catcode`{1  \catcode`}2  \catcode`\ 10 \url@aux } \newcommand*\url@aux[1]{% \setbox0\hbox{\oldurl{#1}}% \ifdim\wd0>\linewidth   \strut  \ \vbox{% \parindent=0pt \kern-\lineskip \raggedright \strut\oldurl{#1}% }% \else \hskip0pt plus\linewidth \penalty0 \box0 \fi \endgroup } \makeatother \begin{document} \lipsum[1] Here is some text \url{http://www.example.com/~example/foo/bar/baz/index%20here.html} Here is some text \url{http://example.org/index.html} Short \url{http://example.org} \end{document} 

Edit:
I modified it to try to prevent the underfull hbox as well as deal with specials in the argument better than just using \detokenize.

added 198 characters in body
Source Link
TH.
  • 65.1k
  • 17
  • 169
  • 225
Loading
Source Link
TH.
  • 65.1k
  • 17
  • 169
  • 225
Loading