Text FormattingInline underlined breaking URL

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
mikey
Posts: 7
Joined: Tue Feb 16, 2010 9:02 pm

Inline underlined breaking URL

Post by mikey »

Can anyone tell me how to make a URL that will wrap properly, can be in the middle of a line and is underlined?

I've been searching everywhere and I can't find anything that does all 3.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

mikey
Posts: 7
Joined: Tue Feb 16, 2010 9:02 pm

Inline underlined breaking URL

Post by mikey »

This is the only problem that I haven't been able to figure out myself and it's beyond me. I really need help.

I'll link what I've found out. There's the package hyperref which seems to be to make clickable URLs in Latex documents. It doesn't appear to do underlines (from what I can tell), but puts annoying boxes round them. The package url does wrapping URLs onto a new line but doesn't like being used in the middle of a line of text. However I found a workaround (http://www.tex.ac.uk/cgi-bin/texfaq2html?label=setURL):

Code: Select all

\urldef\faqhome\url{http://www.tex.ac.uk/faq}
\paragraph{This is some text with an address \faqhome but no underline.}
But, it doesn't have underlines and if I just add \underline around \faqhome it no longer wraps and sticks out into the margin.

The only thing I've been able to find on setting the style of a URL us here (http://www.kronto.org/thesis/tips/url-formatting.html) but it doesn't do underlines (and I have no idea what the code means). If someone could tell me how to do this for setting underlined text rather than changing the font:

Code: Select all

\usepackage{url}

%% Define a new 'leo' style for the package that will use a smaller font.
\makeatletter
\def\url@leostyle{%
  \@ifundefined{selectfont}{\def\UrlFont{\sf}}{\def\UrlFont{\small\ttfamily}}}
\makeatother
%% Now actually use the newly defined style.
\urlstyle{leo}
torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

Inline underlined breaking URL

Post by torbjorn t. »

I cannot help you, I'm afraid, but I would like to comment on one thing.
mikey wrote:There's the package hyperref which seems to be to make clickable URLs in Latex documents. It doesn't appear to do underlines (from what I can tell), but puts annoying boxes round them.
The boxes around links are the default behaviour, but that can easily be changed, by adding either

Code: Select all

\hypersetup{pdfborder=false}
or

Code: Select all

\hypersetup{colorlinks=true}
to your preamble.

With the latter option, you can specify the colour the various types of links will have -- see the hyperref manual.

By the way, why does the URLs need to be underlined?
mikey
Posts: 7
Joined: Tue Feb 16, 2010 9:02 pm

Inline underlined breaking URL

Post by mikey »

Hi torbjorn,

Thanks for the reply. I'm experimenting with hyperref as a way to get it working but I'm not getting far with it either. The reason it has to be underlined is it's part of my dissertation and my university has an extremely stupid and rigidly enforced custom referencing system that demands underlined URLs.

I can get a line under the URL with hyperref using:

Code: Select all

\usepackage[pdftex, pdfborderstyle={/S/U/W 1}]{hyperref}
but the line is blue and the text it black, and I'm not sure if it will print yet, also most importantly it's not wrapping properly. It breaks in my document after the http: but the next line runs out of the margin on the right and then there's a single /folder bit for the end of the URL on the 3rd line.

So still needing help.
torbjorn t.
Posts: 162
Joined: Wed Jun 17, 2009 10:18 pm

Inline underlined breaking URL

Post by torbjorn t. »

I see. This will make the line black:

Code: Select all

\usepackage[pdftex,pdfborderstyle={/S/U/W 1},urlbordercolor={0 0 0}]{hyperref}
As for the linebreaks, I have no really good suggestions. I think hyperref can only break the URLs at certain points, like after slashes and by punctuation marks. One way of circumventing the problem is to split the url, like so:

Code: Select all

This is some text with an address \url{http://www.tug.org/applications/hype} \url{rref/manual.html} and an underline.
The split is placed at the point in the URL where it started flowing in to the margin. This of course makes the link in the PDF useless (which you could fix with the \href{}{} command of hyperref), but for printing it doesn't matter.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Inline underlined breaking URL

Post by frabjous »

I played around with this yesterday, but even with the breaklinks options to hyperref loaded, and the ulem package which allows linebreaks in underlining, I still couldn’t make them work together. I think this is something a package maker may need to pay some attention to.

The only kludge I can suggest in the meantime is to manually break the line yourself and use \href rather than \url, e.g.:

Code: Select all

\href{http://the.url/somewhere}{\underline{\texttt{http://the.url}}}\linebreak\href{http://the.url/somewhere}{\underline{\texttt{/somewhere}}}
Post Reply