Optional but encouraged line break + \hfill
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
.
Classical solution of given task is \nobreak\hfil\penalty0 \hfilneg
. If the line is not broken in the \peanlty0
then \hfil
+\hfilneg
results to nothing. If the line is broken in \peanlty0
then \hfil
is used at the end of the first line but \hfilneg
is discarded because it is "discarable item" (from TeX point of view).