1

coming from here: former thread, I focus on one question and provide a MVE. My problem is: I want to color rows in a longtable with the same color via a macro, to differentiate between two different builds, triggered from outside (internal and public version). Doing this via rowcolor, only the first row is colored, others remain in default background color. I could use the command on every row, however I want only to have one command.

Thanks.

The MWE is:

\documentclass[a4paper,11pt,index=totoc]{scrreprt}

% --- out of style file

% \def\isRelease{1}

\usepackage{colortbl} 
\usepackage{array}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage[justification=centering,font=scriptsize,labelfont=bf,position=bottom]{caption}
\usepackage{xifthen} 
\usepackage{color}

\newcommand{\headcol}{\rowcolor{black}}
\newcommand{\internCol}{\rowcolor{yellow}}

\newcommand{\startLongTable}%
{%
  \renewcommand{\arraystretch}{1.2}%
  \setlength\LTleft{0pt plus \textwidth}%
  \setlength\LTright{0pt plus \textwidth}%
}

\newcommand{\tableRowEnd}%
{%
\\ \midrule%
}

\newcommand{\lastTableRowEnd}%
{%
\\ \addlinespace%
}

\newcommand{\preTableHeaderLine}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
            \arrayrulecolor{black}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
\newcommand{\postTableHeaderLine}{\arrayrulecolor{black}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{black}}

\newcommand{\tableIntern}[2]%
{%
  \ifdefined\isRelease%
    #1%
  \else%
    \tableRowEnd%
    \internCol#2%
  \fi%
}

% --- --------------------------

\begin{document}

\startLongTable
\begin{longtable}{ >{\centering}p{0.1\textwidth-2\tabcolsep} 
                                p{0.2\textwidth-2\tabcolsep} 
                                p{0.4\textwidth-2\tabcolsep} 
                   >{\centering}p{0.2\textwidth-2\tabcolsep}
    >{\centering\arraybackslash}p{0.1\textwidth-2\tabcolsep}
                 }
\preTableHeaderLine
\headcol \color{white} Field & \color{white} Name & \color{white} Description & \color{white} Format & \color{white} \# \\
\postTableHeaderLine \addlinespace
\endhead
1 & Field1 & Name1 & A & 2 \tableRowEnd
2 & Field2 & Name2 & B & 3
\tableIntern{\lastTableRowEnd}
{
3 & Field3 & Name3 & C & 3 \tableRowEnd
4 & Field4 & Name4 & C & 4 \lastTableRowEnd
}
\bottomrule
\caption{stackoverflow MWE}
\label{table:stackoverflowMWE}
\end{longtable}


\end{document}

enter image description here

3 Answers 3

2

You can load xcolor with option [table] instead of colortbl: it extends the latter with a \rowcolors{no of 1st coloured row}{odd rows colour}{even rows colour} command which does what you want if you choose the same colour for even and odd rows.

Just a comment aside: I don't think the horizontal rules between coloured rows look very nice. You might as well replace them with \addlinespace. Also, if you want horizontal rules without the white strips, you can set \above/belowrulesep to 0pt and add some (coloured) padding with the cellspace package. Last point: in my opinion, the white column heads on a black background are more readable with a bold font.

\documentclass[a4paper,11pt,index=totoc]{scrreprt}

% --- out of style file

% \def\isRelease{1}

\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage[justification=centering,font=scriptsize,labelfont=bf,position=bottom]{caption}
\usepackage{xifthen}

\newcommand{\headcol}{\rowcolor{black}}
\newcommand{\internCol}{\rowcolor{yellow}}

\newcommand{\startLongTable}%
{%
  \renewcommand{\arraystretch}{1.2}%
  \setlength\LTleft{0pt plus \textwidth}%
  \setlength\LTright{0pt plus \textwidth}%
}

\newcommand{\tableRowEnd}%
{%
\\ \midrule%
}

\newcommand{\lastTableRowEnd}%
{%
\\ \addlinespace%
}

\newcommand{\preTableHeaderLine}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
            \arrayrulecolor{black}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
\newcommand{\postTableHeaderLine}{\arrayrulecolor{black}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{black}}

\newcommand{\tableIntern}[2]%
{%
  \ifdefined\isRelease%
    #1%
  \else%
    \tableRowEnd%
    \internCol#2%
  \fi%
}

% --- --------------------------

\begin{document}

\startLongTable\rowcolors{2}{yellow}{yellow}
\begin{longtable}{ >{\centering}p{0.1\textwidth-2\tabcolsep}
                                p{0.2\textwidth-2\tabcolsep}
                                p{0.4\textwidth-2\tabcolsep}
                   >{\centering}p{0.2\textwidth-2\tabcolsep}
    >{\centering\arraybackslash}p{0.1\textwidth-2\tabcolsep}
                 }
\preTableHeaderLine
\headcol \color{white}\bfseries Field & \color{white}\bfseries Name & \color{white}\bfseries Description & \color{white}\bfseries Format & \color{white}\bfseries \# \\
\postTableHeaderLine \addlinespace
\endhead
1 & Field1 & Name1 & A & 2 \tableRowEnd
2 & Field2 & Name2 & B & 3
\tableIntern{\lastTableRowEnd}
{
3 & Field3 & Name3 & C & 3 \tableRowEnd
4 & Field4 & Name4 & C & 4 \lastTableRowEnd
}
\hiderowcolors
\bottomrule
\caption{stackoverflow MWE}
\label{table:stackoverflowMWE}
\end{longtable}

\end{document} 

enter image description here

Edit:

Another code, playing with the \showRowcolors and \hideRowcolors switches to activate and deactivate row colouring:

\documentclass[a4paper,11pt,index=totoc]{scrreprt}
    % --- out of style file

    % \def\isRelease{1}

    \usepackage[table]{xcolor}
    \usepackage{array}
    \usepackage{longtable}
    \usepackage{booktabs}
    \usepackage[justification=centering,font=scriptsize,labelfont=bf,position=bottom]{caption}
    \usepackage{xifthen}

    \newcommand{\headcol}{\rowcolor{black}}
    \newcommand{\internCol}{\rowcolor{yellow}}

    \newcommand{\startLongTable}%
    {%
      \renewcommand{\arraystretch}{1.2}%
      \setlength\LTleft{0pt plus \textwidth}%
      \setlength\LTright{0pt plus \textwidth}%
    }

    \newcommand{\tableRowEnd}%
    {%
    \\ \midrule%
    }

    \newcommand{\lastTableRowEnd}%
    {%
    \\ \addlinespace%
    }

    \newcommand{\preTableHeaderLine}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
\specialrule{\belowrulesep}{0pt}{0pt}%
}
    \newcommand{\postTableHeaderLine}{\arrayrulecolor{black}\specialrule{\aboverulesep}{0pt}{0pt}%
                \specialrule{\lightrulewidth}{0pt}{0pt}%
                }

    \newcommand{\tableIntern}[2]%
    {%
      \ifdefined\isRelease%
        #1%
      \else%
        \tableRowEnd%
        \internCol#2%
      \fi%
    }

    % --- --------------------------

    \begin{document}
    \startLongTable\rowcolors{2}{yellow}{yellow}
    \begin{longtable}{ >{\centering}p{0.1\textwidth-2\tabcolsep}
                                    p{0.2\textwidth-2\tabcolsep}
                                    p{0.4\textwidth-2\tabcolsep}
                       >{\centering}p{0.2\textwidth-2\tabcolsep}
        >{\centering\arraybackslash}p{0.1\textwidth-2\tabcolsep}
                     }
    \preTableHeaderLine
    \headcol \color{white}\bfseries Field & \color{white}\bfseries Name & \color{white}\bfseries Description & \color{white}\bfseries Format & \color{white}\bfseries \# \\
    \postTableHeaderLine% \addlinespace
    \endhead
    \hiderowcolors 1 & Field1 & Name1 & A & 2 \tableRowEnd
    2 & Field2 & Name2 & B & 3
    \tableIntern{\lastTableRowEnd}
    {
    3 & Field3 & Name3 & C & 3 \tableRowEnd
    4 & Field4 & Name4 & C & 4 \tableRowEnd
    }
    \hiderowcolors 5 & Field5 & Name5 & D & 2 \tableRowEnd
    \showrowcolors 6 & Field6 & Name6 & E & 3\lastTableRowEnd
        \hiderowcolors
    \bottomrule
    \caption{stackoverflow MWE}
    \label{table:stackoverflowMWE}
    \end{longtable}

    \end{document} 

enter image description here

3
  • Thanks for the further hints about the table display. I agree with the bold header fonts, this is indead better to read. Unfortunatly you color the whole table with your idea, what was not what I intented, as only the "intern"-part should be colored. Coming to speak about the horizontal rules: at the beginning i tried to avoid them at all. However some collegues found tables hard to read when they had numerous lines without the rules. Jul 5, 2017 at 10:23
  • @buffo: I've added another example showing to activate or de-activate row colouring on specific rows (it's a switch). For the lines, just a question: wouldn't \addlinespace[some length] be as clear as rules, and more elegant?
    – Bernard
    Jul 5, 2017 at 13:44
  • @Bernard I have edit and........I upvote you. :-)
    – Sebastiano
    Mar 30, 2018 at 21:18
2

I wouldn't put tabular lines in an argument. This is rather fragile. I would use a syntax like \startrelease ... \stoprelease.

Beside this: If your \tableIntern part ends with a different line command then something like this could work:

\documentclass[a4paper,11pt,index=totoc]{scrreprt}

% --- out of style file

% \def\isRelease{1}

\usepackage{colortbl}
\usepackage{array}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage[justification=centering,font=scriptsize,labelfont=bf,position=bottom]{caption}
\usepackage{xifthen}
\usepackage{color}

\newcommand{\headcol}{\rowcolor{black}}
\newcommand{\internCol}{\rowcolor{yellow}}

\newcommand{\startLongTable}%
{%
  \renewcommand{\arraystretch}{1.2}%
  \setlength\LTleft{0pt plus \textwidth}%
  \setlength\LTright{0pt plus \textwidth}%
}

\newcommand{\tableRowEnd}%
{%
\\ \midrule%
}

\newcommand{\lastTableRowEnd}%
{%
\\ \addlinespace%
}

\newcommand{\preTableHeaderLine}{\arrayrulecolor{black}\specialrule{\heavyrulewidth}{\abovetopsep}{0pt}%
            \arrayrulecolor{black}\specialrule{\belowrulesep}{0pt}{0pt}%
            \arrayrulecolor{black}}
\newcommand{\postTableHeaderLine}{\arrayrulecolor{black}\specialrule{\aboverulesep}{0pt}{0pt}%
            \arrayrulecolor{black}\specialrule{\lightrulewidth}{0pt}{0pt}%
            \arrayrulecolor{black}}

\newcommand{\tableIntern}[2]%
{%
  \ifdefined\isRelease%
    #1%
  \else%
    \gdef \tableRowEnd{\\\midrule\internCol}%
    \gdef \lastTableRowEnd{\gdef\tableRowEnd{\\\midrule}\\\addlinespace}%
    \tableRowEnd%
    \internCol#2%
  \fi%
}

% --- --------------------------

\begin{document}

\startLongTable
\begin{longtable}{ >{\centering}p{0.1\textwidth-2\tabcolsep}
                                p{0.2\textwidth-2\tabcolsep}
                                p{0.4\textwidth-2\tabcolsep}
                   >{\centering}p{0.2\textwidth-2\tabcolsep}
    >{\centering\arraybackslash}p{0.1\textwidth-2\tabcolsep}
                 }
\preTableHeaderLine
\headcol \color{white} Field & \color{white} Name & \color{white} Description & \color{white} Format & \color{white} \# \\
\postTableHeaderLine \addlinespace
\endhead
1 & Field1 & Name1 & A & 2 \tableRowEnd
2 & Field2 & Name2 & B & 3
\tableIntern{\lastTableRowEnd}
{
3 & Field3 & Name3 & C & 3 \tableRowEnd
4 & Field4 & Name4 & C & 4 \lastTableRowEnd
}
5 & text & Name3 & C & 3 \tableRowEnd
6 & test\\
\bottomrule
\caption{stackoverflow MWE}
\label{table:stackoverflowMWE}
\end{longtable}


\end{document}
1
  • Indead, handling the table rows to the argument is not really necassary, as working with your mentioned /start -> /stop will also do the thing. However, the idea with re-defining the command does the job. As a downside, due to table style I have to care about four different table row endings now. Jul 5, 2017 at 10:16
1

An idea is to create a "phantom extra column" and give an empty multirow command inside it with only the row color... If you find difficulties... ask me to help you... (I tried a little but it is a little complicated to get in your code). I think you can do it very easy (You already know how your code works)

3
  • I do not get exactly where you want to put this "phantom extra column". I cannot access the table data directly, as the occurrences of this case are numerous and the data itself can be many rows. Are you going to put that in the macro? Jul 5, 2017 at 7:07
  • 1
    I thought you can manually add a column left or right... or where you like... If you can't add a line it needs much more than I thought... But I will think about it. This way I think I would have to change just the InternCol to multirow...
    – koleygr
    Jul 5, 2017 at 7:12
  • A solution I am trying to develop is this \cptforeach[\] \x \in #2 \do{ \rowcolor{yellow}\x}% but doesn't work yet.... (Sorry ... wrote in Greek before)
    – koleygr
    Jul 5, 2017 at 7:54

You must log in to answer this question.

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