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.

7
  • Use the url package. And welcome to TeX.SX!! Commented Oct 9, 2013 at 4:28
  • 2
    @Sean: Please note the OP is using plain-tex.
    – morbusg
    Commented Oct 9, 2013 at 4:44
  • 2
    @morbusg I wonder if you have a 'true plain' solution: I've taken the easy way out and used url with miniltx :-)
    – Joseph Wright
    Commented Oct 9, 2013 at 7:01
  • Are the URL's to be clickable?
    – morbusg
    Commented Oct 9, 2013 at 7:40
  • URLs are not to be clickable. This is for printing on paper! :-) Commented Oct 9, 2013 at 16:07

4 Answers 4

2

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
4
  • 1
    This could really mess up kerning/typography, but then again it's a URL. (In case you care.) Commented Oct 9, 2013 at 4: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. Commented Oct 9, 2013 at 19:30
  • 1
    After adding the tolerance and emergencystretch from @Joseph Wright's answer, this works perfectly. Commented Oct 10, 2013 at 4:20
  • let me suggest you to change the accepted answer to award it to Joseph...
    – jarnosz
    Commented Apr 17 at 23:19
5

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.

7
  • 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
    Commented Oct 9, 2013 at 7: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. Commented Oct 9, 2013 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: handyfloss.wordpress.com/2006/11/30/tex-capacity-exceeded-error Commented Oct 9, 2013 at 19:28
  • @IronPillow Do you get that with exactly what I put as an example?
    – Joseph Wright
    Commented Oct 9, 2013 at 20:41
  • 1
    Using the suggested tolerance and emergencystretch solves the black-rectangle and ragged-margin problems. Commented Oct 10, 2013 at 2:41
2

The chapter 14 "How TEX Breaks Paragraphs into Lines" in TeXbook explains the hyphenation is just TeX inserts \discretionary{\char\hyphenchar\font}{}{} at every allowed word break when \hyphenchar is a number from 0-255, so set it to -1 disables hyphenation, and now we want to "manually" insert \discretionary{}{}{} between every characters. This seems scary, but it is easy with plain TeX macros, not even the token register is need.

\def\ub@{\discretionary{}{}{}}
\def\appe@#1#2\eol{\def\g{#2}%
\ifx\g\empty #1\else\ub@#1\appe@#2\eol\fi}
\def\protol@#1://#2\eol{#1:\kern-1.5pt/\kern-2.5pt/\appe@#2\eol}
\def\url#1{\begingroup\count0=\the\hyphenchar\font%
\hyphenchar\font=-1%
\protol@#1\eol
\hyphenchar\font=\count0\endgroup}

The work horse is the \appe@ macro, which takes the first character in the string as #1 and the rest of the string until token \eol as #2, and loops to insert \ub@ between them until #2 becomes empty. Note the use of % to prevent extra whitespace occurs in the result. The \protol@ macro is just something I use to prevent breaking at the protocol part of the url (https:// for example), and make the :// a bit more compact. And \begingroup does not reset \hyphenchar so remember to set it back.

[1] American Cornhole League. ``American Cornhole League Rules and
Regulations,'' November 13, 2019. Available:
\url{https://mysqlvm.blob.core.windows.net/acl-docs/ACL-Rules-Regs-2019-2020.pdf}.
[Accessed: April 17, 2024]\par
[2] American Cornhole Organization. ``Cornhole Rules,'' [online document].
Available:
\url{https://americancornhole.com/downloads/ACO-Season16/ACO-Cornhole-Rules-Sheet1.pdf}.
[Accessed: April 17, 2024]\par

used in citation at 10 point

If the long url spans the whole line, it is a good idea to add extra glue around / so TeX can stretch the line a little bit to make it fit. Note that the glue itself would permit TeX to line break at it.

\def\appe@#1#2\eol{\def\g{#2}%
\ifx\g\empty #1\else\ifx#1/\hskip-.5pt plus 1pt/\hskip-1pt plus 1pt%
\else\discretionary{}{#1}{#1}\fi\appe@#2\eol\fi}

citation at 12pt

1
  • 1
    how neat seeing someone else using Concrete!
    – jarnosz
    Commented Apr 17 at 23:18
2

We can create a macro which inserts \hskip0pt plus1pt after each printed character. This space is breakable and slightly stretchable, so the paragraph justification is kept at both margins and the long url's are breakable at arbitrary place.

\newcount\urlnum
\def\url#1{\global\advance\urlnum by1 $^{\the\urlnum}$%
   \bgroup\tt \urlA#1\end}
\def\urlA#1{\ifx#1\end\egroup\else
   \string#1\hskip0pt plus1pt\relax\expandafter\urlA\fi}

\hsize=7cm
\noindent Test: 
\url{http://www.aaa.com/remainder}
\url{http://www.bbb.com/remainder_again}
\url{http://www.ccc.com/ending}

\bye

You must log in to answer this question.

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