1

In the first table I achieved a heat map for specific columns and rows. I want to adapt this for the second table (Labeled Table 1) for row items Nr.1 and Nr.2 and columns 1 to 5 (no bg color), the last column N also no bg color. What's the best way to achieve this?

\documentclass[doc,natbib,floatsintext,12pt,noextraspace]{apa6}
\usepackage[all]{nowidow} 
\usepackage[document]{ragged2e}
\usepackage[english]{babel} 
\usepackage[obeyspaces]{url}
\usepackage[T1]{fontenc} 
\usepackage[tracking=true]{microtype}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,siunitx,caption} 
\usepackage{array}
\usepackage{enumitem}
\usepackage{flafter}
\usepackage{float}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{makecell} 
\usepackage{multirow}
\usepackage{pdflscape} 
\usepackage{scrextend}
\usepackage{setspace}
\usepackage{tabu}
\usepackage{tabularx} 
\usepackage{textcomp}
\usepackage{threeparttable}
\usepackage{tocloft}  
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{hhline}
\usepackage{pgf}
\doublespacing{}
\raggedbottom{} 
\renewcommand{\theadfont}{\normalfont} 
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage{fbb} 
\usepackage[libertine]{newtxmath}

\def\colorModel{hsb} %You can use rgb or hsb
\newcommand\ColCell[1]{
  \pgfmathparse{#1<70?1:0}  %Threshold for changing the font color into the cells
    \ifnum\pgfmathresult=0\relax\color{black}\fi
\pgfmathsetmacro\compA{0.45-#1/270}   %Component R or H
\pgfmathsetmacro\compB{#1/100}           %Component G or S
\pgfmathsetmacro\compC{0.3+1}             %Component B or B
\edef\x{\noexpand\centering\noexpand\cellcolor[\colorModel]{\compA,\compB,\compC}}\x #1} 
\newcolumntype{E}{>{\collectcell\ColCell}m{0.4cm}<{\endcollectcell}}  %Cell width

\begin{document}
\newcommand\items{3}   %Number of classes
\arrayrulecolor{white} %Table line colors
\noindent\begin{tabular}{c*{\items}{|E}|r}
\multicolumn{1}{c}{} & 
\multicolumn{1}{c}{A} & 
\multicolumn{1}{c}{B} & 
\multicolumn{1}{c}{C} & 
\multicolumn{1}{c}{N} \\ \hhline{~*\items{|-}|}
Row 1  & 90  & 0  & 10   & 12  \\ 
Row 2  & 10   & 80  & 10  & 10 \\ 
Row 3  & 30   & 0   & 70   & 19 \\
Row 4  & 30   & 50   & 70   & 19 \\
Row 5  & 30   & 0   & 70   & 19 \\
\end{tabular}


{\footnotesize
    \begin{spacing}{1.2}
        \setlength\tabcolsep{2pt}
        \begin{longtable}{
                @{}
                >{\raggedright}p{8.3cm}
                >{\centering}p{1.1cm}
                >{\centering}p{1.1cm}
                >{\centering}p{1.1cm}
                >{\centering}p{1.1cm}
                >{\centering}p{1.1cm}
                p{0.5cm}
                @{}}
            % header ------------------------
            \caption{Something Something Amazing} \\
            \midrule  
            \multicolumn{0}{l}{\hspace{-2pt}Variable}&
            \multicolumn{0}{c}{{\footnotesize Variants}}&
            \multicolumn{0}{c}{{\footnotesize }}&
            \multicolumn{0}{c}{{\footnotesize }}&
            \multicolumn{0}{c}{{\footnotesize }}&
            \multicolumn{0}{c}{{\footnotesize }}&
            \multicolumn{1}{c}{}\\ 
            \midrule
            %       \noindent\rule{7.3cm}{0.4pt}\\
            \textbf{Something Text}  & 1 & 2 & 3 & 4 & 5 & N \\
            \midrule\addlinespace[2ex]
            Nr.1 Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit. & 14 & 31 & 25 & 20 & 9 & 64 \\ 
            Nr. 2 Integer aliquet volutpat odio at vulputate. Morbi laoreet, ex eget viverra posuere. & 8 & 30 & 44 & 14 & 5 & 64 \\ 
            \midrule
        \end{longtable}
    \end{spacing}
}
\end{document}

1 Answer 1

1

Probably the following suits your needs. I have added a modified version of the custom defined E type column. The E type column derives from the m type nd is automatically 0.4cm wide. While this suits he first table, in the second table, you originally used a different column specifier. I have therefore added te F type column that accepts an argument as follows:

Definition: \newcolumntype{F}[1]{>{\collectcell\ColCell}#1<{\endcollectcell}}

Usage: F{p{3cm}} or F{c} or F{>{\centering\arraybackslash}p{1.1cm}}

To use the above described new column type in your second table, I have replaced the five occurences of >{\centering}p{1.1cm} with *{5}{F{>{\centering\arraybackslash}p{1.1cm}}}.

Lastly, I have also added a placeholder \shorttitle to make the code compilable. Without the \shorttitle you will get an error message complaining about thie missing command.

enter image description here

\documentclass[doc,natbib,floatsintext,12pt,noextraspace]{apa6}
\usepackage[all]{nowidow} 
\usepackage[document]{ragged2e}
\usepackage[english]{babel} 
\usepackage[obeyspaces]{url}
\usepackage[T1]{fontenc} 
\usepackage[tracking=true]{microtype}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,siunitx,caption} 
\usepackage{array}
\usepackage{enumitem}
\usepackage{flafter}
\usepackage{float}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{makecell} 
\usepackage{multirow}
\usepackage{pdflscape} 
\usepackage{scrextend}
\usepackage{setspace}
\usepackage{tabu}
\usepackage{tabularx} 
\usepackage{textcomp}
\usepackage{threeparttable}
\usepackage{tocloft}  
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{hhline}
\usepackage{pgf}
\doublespacing{}
\raggedbottom{} 
\renewcommand{\theadfont}{\normalfont} 
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\usepackage{fbb} 
\usepackage[libertine]{newtxmath}

\shorttitle{short title}


\def\colorModel{hsb} %You can use rgb or hsb
\newcommand\ColCell[1]{
  \pgfmathparse{#1<70?1:0}  %Threshold for changing the font color into the cells
    \ifnum\pgfmathresult=0\relax\color{black}\fi
\pgfmathsetmacro\compA{0.45-#1/270}   %Component R or H
\pgfmathsetmacro\compB{#1/100}           %Component G or S
\pgfmathsetmacro\compC{0.3+1}             %Component B or B
\edef\x{\noexpand\centering\noexpand\cellcolor[\colorModel]{\compA,\compB,\compC}}\x #1} 
\newcolumntype{E}{>{\collectcell\ColCell}m{0.4cm}<{\endcollectcell}}  %Cell width


\newcolumntype{F}[1]{>{\collectcell\ColCell}#1<{\endcollectcell}}  %Cell width

\begin{document}
\newcommand\items{3}   %Number of classes
\arrayrulecolor{white} %Table line colors
\noindent\begin{tabular}{c*{\items}{|E}|r}
\multicolumn{1}{c}{} & 
\multicolumn{1}{c}{A} & 
\multicolumn{1}{c}{B} & 
\multicolumn{1}{c}{C} & 
\multicolumn{1}{c}{N} \\ \hhline{~*\items{|-}|}
Row 1  & 90  & 0  & 10   & 12  \\ 
Row 2  & 10   & 80  & 10  & 10 \\ 
Row 3  & 30   & 0   & 70   & 19 \\
Row 4  & 30   & 50   & 70   & 19 \\
Row 5  & 30   & 0   & 70   & 19 \\
\end{tabular}


{\footnotesize
    \begin{spacing}{1.2}
        \setlength\tabcolsep{2pt}
        \begin{longtable}{
                @{}
                >{\raggedright}p{8.3cm}
                *{5}{F{>{\centering\arraybackslash}p{1.1cm}}}
                p{0.5cm}
                @{}}
            % header ------------------------
            \caption{Something Something Amazing} \\
            \midrule  
            \multicolumn{0}{l}{\hspace{-2pt}Variable}&
            \multicolumn{0}{c}{{\footnotesize Variants}}&
            \multicolumn{0}{c}{{\footnotesize }}&
            \multicolumn{0}{c}{{\footnotesize }}&
            \multicolumn{0}{c}{{\footnotesize }}&
            \multicolumn{0}{c}{{\footnotesize }}&
            \multicolumn{1}{c}{}\\ 
            \midrule
            %       \noindent\rule{7.3cm}{0.4pt}\\
            \textbf{Something Text}  & 1 & 2 & 3 & 4 & 5 & N \\
            \midrule\addlinespace[2ex]
            Nr.1 Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit. & 14 & 31 & 25 & 20 & 9 & 64 \\ 
            Nr. 2 Integer aliquet volutpat odio at vulputate. Morbi laoreet, ex eget viverra posuere. & 8 & 30 & 44 & 14 & 5 & 64 \\ 
                        Nr.1 Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit. & 14 & 31 & 25 & 20 & 9 & 64 \\ 
            Nr. 2 Integer aliquet volutpat odio at vulputate. Morbi laoreet, ex eget viverra posuere. & 8 & 30 & 44 & 14 & 5 & 64 \\ 
            \midrule
        \end{longtable}
    \end{spacing}
}


\end{document}
6
  • absolutely what I was looking for!! Thank you! Two more minor questions: 1) What type of \centering or \center and in what line could I use to have the numbers also vertically centered? They are now top aligned in the table below. 2) Originally I wanted the numbers with the percentages sign, but "34\%" does not work with the ColCell function, I believe. Or is there a simple way to do this?
    – togedo
    Commented Nov 10, 2019 at 17:11
  • To vertically center the contents, use m instead of p type columns as in \begin{longtable}{ @{} >{\raggedright}m{8.3cm} *{5}{F{>{\centering\arraybackslash}m{1.1cm}}} m{0.5cm} @{}}.
    – leandriis
    Commented Nov 10, 2019 at 18:18
  • Regarding the % signs: To be honset, I would not repeat % in every cell contining an number. Probably you can add this information once to your caption or into the colmmn headers of the orresponding columns.
    – leandriis
    Commented Nov 10, 2019 at 18:20
  • Thanks again! I wasn't sure if repeating the % looks good, thanks for your opinion, looks cleaner just with numbers and I'll put it into the caption. What does m or p stand for? Trying to move the values for the column N a bit to the right, >{\raggedleft}m{0.5cm} prints an error :-(
    – togedo
    Commented Nov 10, 2019 at 18:45
  • p will result in top aligned fixed width columns, while m will give your vertically centered fixed width columns. To add a bit more horizontal space, you can use @{<length>} as shown in the following extremely exaggerated example: \begin{longtable}{ @{} >{\raggedright}m{8.3cm} *{5}{F{>{\centering\arraybackslash}m{1.1cm}}} @{\hspace{2cm}}c @{}}.
    – leandriis
    Commented Nov 10, 2019 at 20:42

You must log in to answer this question.

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