0

I am trying to make place some bold & colored borders around certain cells in a longtable using hhline and multicolumn. I noticed that \setlength\arrayrulewidth has different behavior though if the line is - or = and I was hoping someone could explain why.

Full code:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{caption}[singlelinecheck=off]
\usepackage{longtable}
\setlength{\LTleft}{0pt plus 1fill minus 1fill}
\setlength{\LTright}{\LTleft}
\usepackage{xcolor}
\usepackage{amssymb}
\usepackage{color}
\usepackage{colortbl, hhline}
\usepackage{array}
\usepackage{threeparttablex}
\def\settotextwidth{\renewcommand\TPTminimum{\textwidth}}


\begin{document}
\definecolor{purple}{rgb}{0.694117647058824,0.611764705882353,0.850980392156863}

\newcommand{\boldcolored}{\arrayrulecolor{purple}\setlength\arrayrulewidth{5pt}}
\newcommand{\regcolored}{\arrayrulecolor{purple}}
\newcommand{\resetcolor}{\arrayrulecolor{black}}
\newcommand{\vboldcolored}{{\color{purple}\vline width 5pt}}



\begin{ThreePartTable}


\settotextwidth
\begin{TableNotes}
\setlength\labelsep{0pt}
\footnotesize
\arraybackslash\raggedright


\item
\arraybackslash\raggedright

\item Script code:
\item Tex code:

\end{TableNotes}


\begin{center}
\setlength\labelsep{0pt}
{\large Fruit Table } \\

\vspace{3mm}
{\small testing } \\
\vspace{-3mm}

\end{center}

\setlength{\tabcolsep}{3pt}
\captionsetup[table]{labelformat=empty,skip=3pt, justification=raggedright, width =\textwidth}
\begin{longtable}{p{1.72cm}p{1.72cm}p{1.72cm}p{1.72cm}}


\toprule
& & \multicolumn{2}{c}{Summary} \\
 \cmidrule(lr){3-4}
Category & Fruit & Total & Color\\

\endfirsthead
\endhead
\bottomrule
\addlinespace
\insertTableNotes
\endlastfoot
\midrule
\multicolumn{4}{l}{Least Favorite} \\
\midrule
\multicolumn{1}{!\vboldcolored l !\vboldcolored}{fruit 3}& grape & \multicolumn{1}{!\vboldcolored l !\vboldcolored}{3} & purple \\
\hhline{>{\boldcolored}=>{\regcolored}=>{\boldcolored}->{\regcolored}-}\resetcolor
fruit 4 & pear & 4 & green \\
fruit 5 & orange & 5 & orange \\
\midrule
 Total & — & 12 & — \\
 AVG & — & 4 & — \\
 \midrule
\multicolumn{4}{l}{\vspace*{-5mm}} \\
\midrule
fruit 1 & apple & 1 & red \\
fruit 2 & banana & 2 & yellow \\
\midrule
 Sum ALL & — & 15 & — \\

\bottomrule
\end{longtable}
\end{ThreePartTable}


\end{document}

Using pdflatex my table outputs as:

enter image description here

I don't understand why the \boldcolored command only seems to work on the = line here, but not the - one?

To check if it was an issue with changing the rule width midline, I switched the - to the first position and tried to bold it there, but it did not work. I also moved the thick = line to midline and I was able to change rule width. Output:

enter image description here

code for the second table:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{caption}[singlelinecheck=off]
\usepackage{longtable}
\setlength{\LTleft}{0pt plus 1fill minus 1fill}
\setlength{\LTright}{\LTleft}
\usepackage{xcolor}
\usepackage{amssymb}
\usepackage{color}
\usepackage{colortbl, hhline}
\usepackage{array}
\usepackage{threeparttablex}
\def\settotextwidth{\renewcommand\TPTminimum{\textwidth}}


\begin{document}
\definecolor{purple}{rgb}{0.694117647058824,0.611764705882353,0.850980392156863}

\newcommand{\boldcolored}{\arrayrulecolor{purple}\setlength\arrayrulewidth{5pt}}
\newcommand{\regcolored}{\arrayrulecolor{purple}}
\newcommand{\resetcolor}{\arrayrulecolor{black}}
\newcommand{\vboldcolored}{{\color{purple}\vline width 5pt}}



\begin{ThreePartTable}


\settotextwidth
\begin{TableNotes}
\setlength\labelsep{0pt}
\footnotesize
\arraybackslash\raggedright


\item
\arraybackslash\raggedright

\item Script code:
\item Tex code:

\end{TableNotes}


\begin{center}
\setlength\labelsep{0pt}
{\large Fruit Table } \\

\vspace{3mm}
{\small testing } \\
\vspace{-3mm}

\end{center}

\setlength{\tabcolsep}{3pt}
\captionsetup[table]{labelformat=empty,skip=3pt, justification=raggedright, width =\textwidth}
\begin{longtable}{p{1.72cm}p{1.72cm}p{1.72cm}p{1.72cm}}


\toprule
& & \multicolumn{2}{c}{Summary} \\
 \cmidrule(lr){3-4}
Category & Fruit & Total & Color\\

\endfirsthead
\endhead
\bottomrule
\addlinespace
\insertTableNotes
\endlastfoot
\midrule
\multicolumn{4}{l}{Least Favorite} \\
\midrule
\multicolumn{1}{!\vboldcolored l !\vboldcolored}{fruit 3}& grape & \multicolumn{1}{!\vboldcolored l !\vboldcolored}{3} & purple \\
\hhline{>{\boldcolored}->{\regcolored}=>{\boldcolored}=>{\regcolored}-}\resetcolor
fruit 4 & pear & 4 & green \\
fruit 5 & orange & 5 & orange \\
\midrule
 Total & — & 12 & — \\
 AVG & — & 4 & — \\
 \midrule
\multicolumn{4}{l}{\vspace*{-5mm}} \\
\midrule
fruit 1 & apple & 1 & red \\
fruit 2 & banana & 2 & yellow \\
\midrule
 Sum ALL & — & 15 & — \\

\bottomrule
\end{longtable}
\end{ThreePartTable}


\end{document}
  • 1
    you can't change the rule width mid hhline, there is no way I could make that work, how would you want the bits to line up? – David Carlisle Jun 09 '20 at 22:01
  • @DavidCarlisle I added a little bit more to my question. I suspected it might be because I was changing the rule width midline.. so I tested that by moving the bolded = line to midline position and the bold - to first position and I expected the single line to work and the double line to not work if this were to be the case.. but it was the opposite. Now I am wondering what's the difference between - and =? – Anna Nevison Jun 09 '20 at 22:18
  • @DavidCarlisle also the second picture I added is how I would want the bits to line up – Anna Nevison Jun 09 '20 at 22:20
  • the difference is that essentially - is just a rule but = has to be made with \leaders containing a box with two rules and that box is just made once, the exact behaviour is a bit weird but it's all just accidental code paths it never occurred to me that anyone would try this:-) It's the first time I have seen the issue raised in the 20-something years since I wrote this code..... what would work is setting the rule width (globally in a \noalign} to a different value before a hhline and then setting it back after – David Carlisle Jun 09 '20 at 22:26
  • @DavidCarlisle ohhh, okay I understand- thank you! I am writing an R package to help make LaTeX tables for reports and a feature I would like to add is the ability for users to specify borders of varying thickness, color, double lines, etc. on individual cells/rows/columns in their tables. hhline seemed to have everything I needed so I was just playing around with it and thought this was interesting. But I will definitely try that recommendation- thank you again! – Anna Nevison Jun 09 '20 at 22:57

1 Answers1

1

@DavidCarlisle provided an explanation for this behavior that helped me figure out the solution I was looking for so posting as the answer to close this out.

the difference is that essentially - is just a rule but = has to be made with \leaders containing a box with two rules and that box is just made once

Looking at hhline:

\HH@box creates the box that makes the double line segment.

\def\HH@box#1#2{\vbox{%
  \hrule \@height \arrayrulewidth \@width #1
  \vskip \doublerulesep
  \hrule \@height \arrayrulewidth \@width #2}}

The box is made with two rules and a rule sep. The single line is just \hrule.

So using this info, to make thick/colored box borders around specific cells using just hhline:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage{caption}[singlelinecheck=off]
\usepackage{longtable}
\setlength{\LTleft}{0pt plus 1fill minus 1fill}
\setlength{\LTright}{\LTleft}
\usepackage{xcolor}
\usepackage{amssymb}
\usepackage{color}
\usepackage{colortbl, hhline}
\usepackage{array}
\usepackage{threeparttablex}
\def\settotextwidth{\renewcommand\TPTminimum{\textwidth}}

\begin{document} \definecolor{purple}{rgb}{0.694117647058824,0.611764705882353,0.850980392156863}

\begin{ThreePartTable}

\newcommand{\borderheight}{\dimexpr 2.495pt + \ht\strutbox} \newcommand{\boldcolored}{\arrayrulecolor{purple}\setlength\arrayrulewidth{2.495pt}\setlength\doublerulesep{0.01pt}} \newcommand{\vboldcolored}{{\color{purple}\vline width 5pt height \borderheight}} \newcommand{\resetcolor}{\arrayrulecolor{black}}

\settotextwidth \begin{TableNotes} \setlength\labelsep{0pt} \footnotesize \arraybackslash\raggedright

\item \arraybackslash\raggedright

\item Script code: \item Tex code:

\end{TableNotes}

\begin{center} \setlength\labelsep{0pt} {\large Fruit Table } \

\vspace{3mm} {\small testing } \ \vspace{-3mm}

\end{center}

\setlength{\tabcolsep}{3pt} \captionsetup[table]{labelformat=empty,skip=3pt, justification=raggedright, width =\textwidth} \begin{longtable}{p{1.72cm}p{1.72cm}p{1.72cm}p{1.72cm}}

\toprule & & \multicolumn{2}{c}{Summary} \ \cmidrule(lr){3-4} Category & Fruit & Total & Color\

\endfirsthead \endhead \bottomrule \addlinespace \insertTableNotes \endlastfoot \midrule \multicolumn{4}{l}{Least Favorite} \ \midrule \hhline{>{\boldcolored}=>{\boldcolored}=}\resetcolor \multicolumn{1}{!\vboldcolored l}{fruit 3}& \multicolumn{1}{l !\vboldcolored}{grape} & 3 & purple \ \hhline{>{\boldcolored}=>{\boldcolored}=}\resetcolor fruit 4 & pear & 4 & green \ fruit 5 & orange & 5 & orange \ \midrule Total & — & 12 & — \ AVG & — & 4 & — \ \midrule \multicolumn{4}{l}{\vspace*{-5mm}} \ \midrule fruit 1 & apple & 1 & red \ fruit 2 & banana & 2 & yellow \ \midrule Sum ALL & — & 15 & — \

\bottomrule \end{longtable} \end{ThreePartTable}

\end{document}

Setting the \doublerulesep to near zero (.01 -setting to zero did not work) so that the lines come together as 1. I originally wanted one 5pt thick line so I set the arrayrulewidth to 0.5x5pt-(0.5x(0.01)) = 2.495pt for each so when they came together they would be 5pt. The output of this was what I was looking for and a lot easier than some solutions I saw using tikz for colored boxes around specific cells.

enter image description here