1

Is it possible to define clickable links in a plainTeX document when compiled with pdftex? As far as I can see there is no support in plainTeX for this feature.

dallonsi
  • 1,299
  • 1
  • 8
  • 29

1 Answers1

1

For creating clickable links in pdf documents generated from a TeX (plainTex) document you can use this code:

\newif\ifpdfmode
\ifx\pdfoutput\undefined
\else
    \ifnum\pdfoutput>0 \pdfmodetrue\fi
\fi

\def\url#1{%
    % turn off the special meaning of ~ inside \url{}.
    \begingroup\catcode`\~=12\catcode`\_=12\relax
    \ifpdfmode
        \pdfstartlink user{
            /Subtype /Link
            % w/o this you get an ugly box around the URL.
            /Border [ 0 0 0 ]   % radius, radius, line thickness
            /A <<
                /Type /Action
                /S /URI
                /URI (https://#1)
        >>
        }%
        {\tt#1}%
        \pdfendlink{}%
    \else
        %{\tt https\negthinspace:\negthinspace/\negthinspace/#1}%
        {\tt#1}%
    \fi
    \endgroup}

that you can save in a file named lib/url.sty. Note that it injects some pdf code because links are not natively supported by TeX (even when using the pdftex compiler).

Once done it's just a question of using the macro url in your TeX code:

\input lib/url.sty

My preferred site is \url{stackoverflow.com}!

The macro \url works also when the document is not compiled with pdftex. In this case the conditional ifpdfmode will be set to false by the compiler and a simple text formatted with the \tt font will be rendered instead.

You can find a "live" example here: https://github.com/madrisan/cv

Davide Madrisan
  • 1,969
  • 2
  • 14
  • 22
  • the links in your CV are not clickable when opening the file in github (https://github.com/madrisan/cv/blob/master/dmadrisan_cv_en.pdf), shame on github :p – dallonsi Mar 03 '22 at 16:55
  • could you explain roughly what your code does? – dallonsi Mar 03 '22 at 16:56
  • 1
    It injects some postscript code that make the parameter #1 of the macro `\url` a clickable URL. I forgot to say that a prefix `https://` is added. This should be not an issue nowadays. If you want to support `http://` links you can easily modify the macro. – Davide Madrisan Mar 03 '22 at 17:57