2

The > and < preamble options of the array package (and possibly other table packages) allow automatically inserting text and commands before and after each cell of a column, respectively.

Can they be used to insert things like \hyperlink{foo}{ before and } after each cell, or something like that? Attempts so far:

  • >{ \hyperlink{foo}{ } c <{ } } causes an error because the braces are unbalanced.

  • Escaping the braces by \{ just inserts braces into the PDF.

  • >{ \begin{hyperlink}{foo} } c <{ \end{hyperlink} } interestingly causes sharelatex to complain about the closed brace between begin and end, but does not cause a latex error.

    The problem though is that this results in \begin{hyperlink}{foo}text in cell\end{hyperlink}, adding a hyperlink only to the first letter "t", whereas I need the result to be \begin{hyperlink}{foo}{text in cell}\end{hyperlink}, and for some reason not even surrounding every cell entry by braces manually helps.

Is there a way to achieve this?

root
  • 418

2 Answers2

3

You could use collcell. If you want to use the column type p, you need to add a width, so for the time being I chose c.

\documentclass{article}
\usepackage{collcell}
\usepackage{hyperref}
\newcommand{\Foo}[1]{\hyperlink{foo}{#1}}
\newcolumntype{H}{>{\collectcell\Foo}c<{\endcollectcell}}
\begin{document}
\begin{tabular}{H}
a\\
b\\
\end{tabular}
\end{document}

enter image description here

  • Would also something like >{\collectcell\hyperlink{foo}}... work, without defining a new command for every possible first argument of \hyperlink? – root Oct 30 '19 at 13:19
  • 1
    @root Honestly, I do not know. According to what I know this might be the simplest option. –  Oct 30 '19 at 13:20
  • 1
    @root \collectcell\hyperlink{foo} clearly can't work, but I assume \collectcell{\hyperlink{foo}} will – David Carlisle Oct 30 '19 at 13:57
  • @DavidCarlisle Thanks! Yes it does. \newcolumntype{H}{>{\collectcell{\hyperlink{foo}}}c<{\endcollectcell}}. There is a follow up question that is waiting for an answer, I believe. –  Oct 30 '19 at 14:00
3

If all items in a column share the target, you just need to specify it: an additional pair of braces is all you need in the \collectcell part.

\documentclass{article}
\usepackage{collcell}
\usepackage{hyperref}

\newcolumntype{h}[1]{%
  >{\collectcell{\hyperlink{#1}}}c<{\endcollectcell}%
}

\begin{document}

\begin{tabular}{h{foo} h{bar}}
to foo 1 & to bar 1 \\
to foo 2 & to bar 2
\end{tabular}

\newpage\hypertarget{foo}{This is foo}
\newpage\hypertarget{bar}{This is bar}

\end{document}

If you need to specify different targets, brace the items together with their target.

\documentclass{article}
\usepackage{collcell}
\usepackage{hyperref}

\newcommand{\makehyperlink}[1]{\makehyperlinkaux#1}
\newcommand{\makehyperlinkaux}[2]{\hyperlink{#1}{#2}}

\begin{document}

\begin{tabular}{>{\collectcell\makehyperlink}c<{\endcollectcell}}
{foo}{a}\\
{bar}{b}\\
\end{tabular}

\newpage\hypertarget{foo}{FOO}

\newpage\hypertarget{bar}{BAR}

\end{document}
egreg
  • 1,121,712