4

Minimal example:

\documentclass{article}
\begin{document}
https://path.to.really.long/url/that/contains/no/dashes/but/instead/a/whole/lot/of/forward/slashes/which/makes/it/exceed/the/page/width
\end{document}

Using XeLaTeX, I am trying to use a large number of unformatted URLs without using the hyperref package, and would like the URLs to break at /, as some of them don’t contain -. How could I do this?

EDIT:

I’ll try to be more specific. What I have is this:

http://link1.com/long/link/...
‹lots of random text›
http://link2.com/long/link/...
‹lots of random text›
http://link3.com/long/link/...
...
‹lots of random text›
...
http://link573.com/long/link/...

It’s a team project, and I want the source to be able to be read, edited and easily understood by others who don’t use TeX. I have a master preamble/layout document and there I include all the other unformatted plaintext files.

  • 1
    Are you allowed to use the url package, though? – Mico Mar 15 '15 at 13:20
  • Sure, the only constraint is that all the links should be in a roman font (and preferably not be clickable if that’s possible). – user74027 Mar 15 '15 at 13:22
  • By "roman" font, do you mean the serif font that's used as the main font of the document? – Mico Mar 15 '15 at 13:23
  • Exactly! The links are entered without formatting in the source. – user74027 Mar 15 '15 at 13:25
  • You've informed us that \url{...} is not acceptable because your co-authors "don't use TeX". What typesetting system do they use? Would they truly be unduly burdened by the occasional bit of TeX-style markup? – Mico Mar 15 '15 at 14:03
3

Are you allowed to load the url package, though? If so, encase the URL in a \url{...} macro. Issue the instruction \urlstyle{rm} to ensure that the font used for the URL string is the same as the main text font.

The following code compiles equally well under pdfLaTeX, XeLaTeX, and LuaLaTeX.

enter image description here

\documentclass{article}
\usepackage{url}
\urlstyle{rm} % roman font
\begin{document}
\noindent
\url{https://path.to.really.long/url/that/contains/no/dashes/but/instead/a/whole/lot/of/forward/slashes/which/makes/it/exceed/the/page/width}
\end{document}

Addendum: If using LuaLaTeX instead of XeLaTeX is an option for you, you could define a Lua function (to be registered with the process_input_buffer callback) that replaces all occurrences of / with \slash: The TeX macro \slash, unsurprisingly, inserts a slash symbol while allowing a line break immediately following this symbol. (The function also takes care not to allow a line break after the first of two consecutive / symbols, as such cases may occur quite frequently, viz., \http:// and \https://.)

I trust this setup meets your objective of not having to encase URL strings in \url directives and still get line breaks following slash symbols.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode}
\begin{luacode}
local function slash_to_slash ( ll )
      ll = string.gsub ( ll, "/", "\\slash " )
      ll = string.gsub ( ll, "\\slash \\slash ", "/\\slash " )
      return ll
end
luatexbase.add_to_callback("process_input_buffer", 
     slash_to_slash, 
     "convert slash symbols to slash macros")
\end{luacode}
\begin{document}

https://path.to.really.long/url/that/contains/no/dashes/but/instead/a/whole/lot/of/forward/slashes/which/makes/it/exceed/the/page/width

\end{document}
  • Is there no way to just have a document of links lacking formatting, and have them appear as normal text, and breaking as normal text? – user74027 Mar 15 '15 at 13:29
  • @svenper - I'm not sure I follow you. The URL string is rendered as "normal text", and as far as I can tell there's no special formatting applied to the URL string. – Mico Mar 15 '15 at 13:31
  • I’ll update the OP with more info. – user74027 Mar 15 '15 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy