11

I'm forced to avoid the url package in a proceedings paper (!) but my bibliography includes a long URL. I already

\newcommand{\url}[1]{{\tttext{#1}}}

but the URL spills over to the margin as it isn't hyphenated. I would rather not change the bibliography manually to add the hyphens (my current solution) but rather define the new \url command to put in the \- commands before dots and forward slashes. I've seen a few related questions but close enough for my TeX skills...

Also, is there a way to make the hyphenation happen without an actual hyphen?

EDIT: sorry, I should have said, that not only the url package is disallowed...but I cannot include any extra package. the packages that are included are

graphicx multicol footmisc amsmath amsfonts

So I guess this is a plainTeX question.

3

2 Answers 2

9

Without any package, your only hope is that they use a fairly recent TeX distribution:

\newcommand{\url}[1]{%
  \begingroup
  \ttfamily
  \begingroup\lccode`~=`/\lowercase{\endgroup\def~}{/\discretionary{}{}{}}%
  \begingroup\lccode`~=`.\lowercase{\endgroup\def~}{.\discretionary{}{}{}}%
  \catcode`/=\active\catcode`.=\active
  \scantokens{#1\noexpand}%
  \endgroup
}

This will allow breaks after periods and slashes.

If you have some special character in your URLs, they can be taken care of, just present some examples.

2
  • I noticed the following \raggedright behavior with both your answer as well as the url package; Namely, while breaks are "allowed" after slashes, it will frequently prefer to move the whole url down to the next line, in preference to breaking at a slash (if the whole url fits on a single line). Is that expected behavior? Sep 29, 2014 at 19:34
  • 1
    @StevenB.Segletes Yes, with \raggedright hyphenation is essentially suppressed. Use \RaggedRight from ragged2e. A different strategy might be using {/\penalty0 } in the definition instead of {/\discretionary{}{}{}}
    – egreg
    Sep 29, 2014 at 19:41
7

You can adapt the solution from Option to break urls with carriage-return symbol? to define \url:

enter image description here


If you prefer the use a dash at the end of the line then replace the line \discretionary{-}{}{} which yields:

enter image description here

but note that this can be confusing as it is difficult to tell if the trailing dash is part of the URL or not. This is not an issue if the link is to be clicked on with online viewing, but for printed version this may be an issue.


Alternatively, if you use \discretionary{\usebox\MyArrowBox}{}{} then you can have a special character (I used \hookleftarrow, but you can adapt this to use any other symbol) at the end of the line to indicate the continued text so that there is no confusion with a dash.

enter image description here


Code:

\documentclass{article}
\usepackage{hyphenat}
\usepackage{xstring}
\usepackage{forloop}

\newsavebox\MyArrowBox%
\sbox\MyArrowBox{$\hookleftarrow$}%
\makeatletter%
\newcommand*{\BreakableChar}{%
  \leavevmode%
  \prw@zbreak%
  \discretionary{}{}{}%
  %\discretionary{-}{}{}% If want a dash
  %\discretionary{\usebox\MyArrowBox}{}{}%
  \prw@zbreak%
}%

\newcounter{index}%
\newcommand{\url}[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/50777/how-to-emulate-url-hyphenating-without-using-the-url-package}%

\begin{document}
\parbox{4cm}{\url{\MyUrl}}

\bigskip
\parbox{7cm}{\url{\MyUrl}}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .