pdftexpdftex

clickable links in plainTeX document


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.


Solution

  • 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