2

With url.sty, I would like to use spaces in URLs to indicate possible points for linebreak but without the spaces actually being displayed in the output. This idea is actually inspired by this little passage from the url.sty package documentation:

Note that it seems logical to allow the sole option “[spaces]” to let input spaces indicate break points, but not to display them in the output. This would be easy to implement, but is left out to avoid(?) confusion.

Well, so how would I implement it? :)

(Note that the questionmark was in the original ...)

MWE:

\documentclass{article}
\usepackage{url}

\begin{document}
So, here we go: 
\url{http://www.this-is-a-long.com/long long long long long long
long/domain/name?with=a &lot=variables &that=I &would=like &to=be &able=to
&break=at &space=characters}
\end{document}

Edit 1:

What I do not want is the spaces really to appear in the displayed URL. This is why the obeyspaces package option of url.sty cannot be used. What I mean is to use spaces in the source code only to indicate possible break points without them appearing in the output.

Activating spaces as breakpoints is precisely the purpose of the spaces option. So it would be natural to use

\usepackage[spaces]{url} 

to achieve the functionality I want: spaces as breakpoints but not being displayed in the output. However, as the documentation, page 2, states the spaces option can only be used in combination with obeyspaces but not stand alone which would be the logical configuration for the functionality I want. Please see the above quotation from the manual, which suggests that not using obeyspaces but using spaces is deliberately deactivated to "avoid" confusion athough it would be easy to implement.

So my question is: How can it "easily" be implemented?

2 Answers 2

3

This combination seems to work. The obeyspaces options activates the handling of spaces, the redefinition than gives it the wanted output/action. The spaces option gives \Url@sppen a "breakable" value, but is not really needed. E.g. \def\Url@space{\penalty\UrlBreakPenalty} would work too.

\documentclass{article}
\usepackage[obeyspaces,spaces]{url}

\textwidth1cm
\makeatletter
\def\Url@space{\penalty\Url@sppen}
\makeatother
\begin{document}
So, here we go:


\url{http://www.this-is-a-long.com/long long long long long long
long/domain/name?with=a &lot=variables &that=I &would=like &to=be &able=to
&break=at &space=characters}
\end{document}

enter image description here

5
  • No sorry, it does not produce the desired result as the obeyspaces option makes all the spaces appear in the URL. This is why I asked the question: The idea is about using spaces in the URL only to indicate possible break points but without them actually being displayed in the output. Please refer to the url package manual page 2. Mar 29, 2017 at 8:42
  • I edited my original question above to make my point more clearer. Mar 29, 2017 at 8:50
  • In my screenshot there are no spaces in the output (e.g. long long prints as longlong). So where is your problem? Mar 29, 2017 at 8:55
  • OK I'm sorry that's right!! Don't know what I did wrong when I was first testing your solution. I cannot upvote the solution because my voting is 'locked' ... Mar 29, 2017 at 9:10
  • I edited the answer, this should unlock it. Mar 29, 2017 at 9:17
3

A bit stricter than what Ulrike Fischer did:

\documentclass{article}
\usepackage[obeyspaces]{url}


\makeatletter
\def\Url@space{\penalty10}
\def\UrlBreakPenalty{100000}
\def\UrlBigBreakPenalty{100000}
\makeatother

\begin{document}
So, here we go: 
\url{http://www.this-is-a-long.com/long long long long long long
long/domain/name?with=a &lot=variables &that=I &would=like &to=be &able=to
&break=at &space=characters}
\end{document}

This should allow breaks only at spaces (perhaps I forgot one or two penalties).

I did not use the spaces option, because I set the penalties to absolute values and did not use any url-internal variables like \Url@sppen which is set to \UrlBreakPenalty when you use spaces. Therefore the spaces option wouldn't change anything in my answer.

2
  • 1
    You may want to provide a quick discussion of why you're not including the spaces option.
    – Mico
    Mar 28, 2017 at 9:59
  • @Mico The answer to that is, because I set the penalties to numbers and did not use internal variables like \Url@sppen which is set to \UrlBreakPenalty if you call the spaces option. So the spaces option changes the behavior in Ulrike Fischers answer but not in mine.
    – Skillmon
    Mar 28, 2017 at 10:44

You must log in to answer this question.

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