9

I am using plain TeX and want to typeset an appendix that contains several hundred URLs. When a URL goes past the right margin, I would like TeX to stop printing (i.e. not carry into the margin, not print a warning black rectangle) and then pick up the rest of the URL on a new (non-indented) line where it left off.

Something like this, where 1, 2, 3 are akin to footnote numbers:

1: http://www.aaa.com/rem
ainder 2: http://www.bbb.
com/remainder_again  3: h
ttp://www.ccc.com/ending

Is this possible?

I am using a Python script to generate the .tex files, so, if worst comes to worst, I can have Python estimate the content of each line. Unfortunately, I am not using a mono-spaced font, so that could turn pretty ugly.

Thanks in advance.

morbusg
  • 25,490
  • 4
  • 81
  • 162
Iron Pillow
  • 493
  • 2
  • 8

2 Answers2

4

The LaTeX url package can be used with plain TeX using miniltx (or you could copy the minimal code, of course). For example

\input miniltx %
\input url.sty %
\urlstyle{rm}
\def\UrlBreaks{\do\/\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z}
\hsize 4.4cm %
\noindent
1.~\url{http://www.aaa.com/remainder}
2.~\url{http://www.bbb.com/remainder_again}
3.~\url{http://www.ccc.com/ending}
\bye

will give you breaking at any character using the method suggested in the url manual and Forcing linebreaks in \url. Things are dependent on line-width: with very few 'normal' spaces in the example above avoiding overfull boxes is hard, and the equivalent of LaTeX's \sloppy may also be required, e.g.

\tolerance 9999 %
\emergencystretch 3em %

I'm assuming we have to allow breaks everywhere: that is normally not such a great plan if it can be avoided.

To understand what is happening, notice that url (ab)uses math mode to allow breaking 'anywhere', making any breakable URL character into a mathbin character inside the URL.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • One could also use eplain to load url: \input eplain \beginpackages \usepackage{url} \endpackages \enablehyperlinks \url{http://foo/bar} \bye (from the eplain manual). – morbusg Oct 09 '13 at 07:31
  • Yields a "TeX capacity exceeded, sorry" error. I thought the problem might be too many possible breakpoints (see comment to @ChrisS's answer), but even when I reduce the number of "do" macros to three, the error persists. – Iron Pillow Oct 09 '13 at 19:25
  • It appears that something to do with the \url macro could be at fault. This page has lots of examples of that causing the same error: http://handyfloss.wordpress.com/2006/11/30/tex-capacity-exceeded-error/ – Iron Pillow Oct 09 '13 at 19:28
  • @IronPillow Do you get that with exactly what I put as an example? – Joseph Wright Oct 09 '13 at 20:41
  • (1) The exact sample above runs error-free. (2) If I add an extra line above the \bye that inputs my file of 8024 \url macros, it runs error-free. (3) If I repeat (2) but delete the line \hsize 4.4cm then it yields a "capacity exceeded" error. (4) If I split the 8024 URLs into 4 paragraphs, then (3) runs without error. – Iron Pillow Oct 09 '13 at 23:48
  • Curiously, when I remove the period after each of the numbers 1,2,3: (a) the black rectangles are printed at the end of every line and (b) the right margin is ragged. – Iron Pillow Oct 09 '13 at 23:57
  • Using the suggested tolerance and emergencystretch solves the black-rectangle and ragged-margin problems. – Iron Pillow Oct 10 '13 at 02:41
1

Since you're generating the TeX code with Python, one option is to separate every character with \hskip0pt. For example:

\def\a{a\hskip0pt}
\def\b{\a\a\a\a\a\a\a\a\a\a}
\def\c{\b\b\b\b\b\b\b\b\b\b}
\def\d{\c\c\c\c\c\c\c\c\c\c}

\d
ChrisS
  • 13,529
  • This could really mess up kerning/typography, but then again it's a URL. (In case you care.) – Sean Allred Oct 09 '13 at 04:31
  • This worked! But there are two caveats: (1) I had escaped characters like & to {&} in my URLs, but of course I had to unescape and then re-escape them for this solution to work, and (2) Putting \hskip0pt between every character exceeded TeX's memory capacity (I have a LOT of URLs!) but reducing it to every fifth character worked pretty well. There are a few overfull boxes, but I can live with that. – Iron Pillow Oct 09 '13 at 19:30
  • After adding the tolerance and emergencystretch from @Joseph Wright's answer, this works perfectly. – Iron Pillow Oct 10 '13 at 04:20