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.

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.

share|improve this question
    
    
@PeterGrill: no extra packages, see my edit. –  Yossi Farjoun Apr 4 '12 at 20:26
    
@cgnieder: interesting. I'll try it out. –  Yossi Farjoun Apr 4 '12 at 20:26

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
This works perfect. Thanks! –  Yossi Farjoun Apr 5 '12 at 15:31

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}{http://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}
share|improve this answer

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.