1

This is a follow up to Suppress vertical space above and below array?, which works fine when used with an \fbox (black box below), but not with \href (teal box below). My goal is to eliminate the excess vertical space that the href bounding box seems to be placing around the array environment:

enter image description here

If the href bounding box was close to what \fbox uses that would be fine.

Code

\documentclass{article}
\usepackage{array}
\usepackage{environ}
\usepackage{booktabs}% for \addlinespace
\usepackage{hyperref}

\makeatletter
%% https://tex.stackexchange.com/questions/366123/suppress-vertical-space-above-and-below-array
\NewEnviron{mysolidarray}[1]{%
  \begingroup
  \def\arraystretch{0}%
  \extrarowheight=\z@
  \sbox\z@{$\begin{array}[t]{#1}\BODY\end{array}$}%
  \sbox\tw@{$\begin{array}[b]{#1}\BODY\end{array}$}%
  \expandafter\endgroup\expandafter
  \firstlineheight\the\ht\expandafter\z@\expandafter
  \lastlinedepth\the\dp\tw@\relax
  \sbox{\solidarraybox}{$\begin{array}[t]{#1}\BODY\end{array}\m@th$}
  \ht\solidarraybox=\firstlineheight
  \dp\solidarraybox=\dimexpr\dp\solidarraybox-\dp\@arstrutbox+\lastlinedepth\relax
  \box\solidarraybox
}
\makeatother
\newdimen\firstlineheight
\newdimen\lastlinedepth
\newsavebox{\solidarraybox}

\def\MyArrayContentA{\cdot  \\[-2.0ex] \cdot}%
\def\MyArrayContentB{\cdot\cdot  \\[-2.35ex] \cdot  \\[-2.35ex] \cdot\cdot}%

\def\MyArrayA{%
  \begin{array}[t]{@{}c@{}}
    \MyArrayContentA
  \end{array}%
}%
\def\MySolidArrayA{%
  \begin{mysolidarray}{@{}c@{}}
    \MyArrayContentA
  \end{mysolidarray}%
}%

\def\MyArrayB{%
  \begin{array}[t]{@{}c@{}}
    \MyArrayContentB
  \end{array}%
}%
\def\MySolidArrayB{%
  \begin{mysolidarray}{@{}c@{}}
    \MyArrayContentB
  \end{mysolidarray}%
}%


\begin{document}
\fboxsep=0pt 
\begin{tabular}{lll}
\verb|array|&
      \fbox{$\MyArrayA$} \href{xxx}{$\MyArrayA$}
    & \fbox{$\MyArrayB$} \href{xxx}{$\MyArrayB$}
\\\addlinespace[1.0ex]
\verb|mysolidarray|&
      \fbox{$\MySolidArrayA$} \href{xxx}{$\MySolidArrayA$}
    & \fbox{$\MySolidArrayB$} \href{xxx}{$\MySolidArrayB$}
\end{tabular}
\end{document}

1 Answer 1

2

\href isn't really adding extra padding space it's just that the visual link border is added via black magic and doesn't really reflect the tex box dimensions.

If you put an \fbox around the \href you will see that the tex box is still constrained and in fact it seems just using \mbox is enough to give pdftex a hint on the required link border dimensions.

enter image description here


\documentclass{article}
\usepackage{array}
\usepackage{environ}
\usepackage{booktabs}% for \addlinespace
\usepackage{hyperref}

\makeatletter
%% https://tex.stackexchange.com/questions/366123/suppress-vertical-space-above-and-below-array
\NewEnviron{mysolidarray}[1]{%
  \begingroup
  \def\arraystretch{0}%
  \extrarowheight=\z@
  \sbox\z@{$\begin{array}[t]{#1}\BODY\end{array}$}%
  \sbox\tw@{$\begin{array}[b]{#1}\BODY\end{array}$}%
  \expandafter\endgroup\expandafter
  \firstlineheight\the\ht\expandafter\z@\expandafter
  \lastlinedepth\the\dp\tw@\relax
  \sbox{\solidarraybox}{$\begin{array}[t]{#1}\BODY\end{array}\m@th$}
  \ht\solidarraybox=\firstlineheight
  \dp\solidarraybox=\dimexpr\dp\solidarraybox-\dp\@arstrutbox+\lastlinedepth\relax
  \box\solidarraybox
}
\makeatother
\newdimen\firstlineheight
\newdimen\lastlinedepth
\newsavebox{\solidarraybox}

\def\MyArrayContentA{\cdot  \\[-2.0ex] \cdot}%
\def\MyArrayContentB{\cdot\cdot  \\[-2.35ex] \cdot  \\[-2.35ex] \cdot\cdot}%

\def\MyArrayA{%
  \begin{array}[t]{@{}c@{}}
    \MyArrayContentA
  \end{array}%
}%
\def\MySolidArrayA{%
  \begin{mysolidarray}{@{}c@{}}
    \MyArrayContentA
  \end{mysolidarray}%
}%

\def\MyArrayB{%
  \begin{array}[t]{@{}c@{}}
    \MyArrayContentB
  \end{array}%
}%
\def\MySolidArrayB{%
  \begin{mysolidarray}{@{}c@{}}
    \MyArrayContentB
  \end{mysolidarray}%
}%


\begin{document}
\fboxsep=0pt 
\begin{tabular}{lll}
\verb|array|&
      \fbox{$\MyArrayA$} \href{xxx}{$\MyArrayA$}
    & \fbox{$\MyArrayB$} \href{xxx}{$\MyArrayB$}
\\\addlinespace[1.0ex]
\verb|mysolidarray|&
      \fbox{$\MySolidArrayA$} \href{xxx}{$\MySolidArrayA$} \mbox{\href{xxx}{$\MySolidArrayA$}}
    & \fbox{$\MySolidArrayB$} \href{xxx}{$\MySolidArrayB$} \mbox{\href{xxx}{$\MySolidArrayB$}}
\end{tabular}
\end{document}

4
  • Is there any way to modify the array content rather than using an ``\mbox{}` around the \href? Commented Sep 10, 2021 at 8:17
  • @PeterGrill I'll ask Ulrike but as far as I can tell the border padding heuristics are coming from inside pdftex (for example if you use latex/dvips/ps2pdf then the tex box structure is the same but the visible link border is tight in the code as you posted) You could of course turn off the hyperref borders and mark the link area with color or \fbox or whatever (the link rectangles are pretty ugly normally anyway) Commented Sep 10, 2021 at 8:35
  • @PeterGrill chat.stackexchange.com/transcript/message/59110536#59110536 Commented Sep 10, 2021 at 9:00
  • @PeterGrill the reason pdftex does this is so that \href{ZZZ} and \href{...} both make a link box the height of the surrounding line not the height of the content so you get a consistent sized thing to click on. So it's the height of the surrounding box that matters which is why the mbox, being the height of the array, shrinks it. Commented Sep 10, 2021 at 9:04

You must log in to answer this question.

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