Colorear filas de tabla larga mediante macro

Colorear filas de tabla larga mediante macro

viniendo de aquí:hilo anterior, Me concentro en una pregunta y proporciono un MVE. Mi problema es: quiero colorear filas en una tabla larga con el mismo color a través de una macro, para diferenciar entre dos compilaciones diferentes, activadas desde afuera (versión interna y pública). Al hacer esto a través de color de fila, solo se colorea la primera fila, las demás permanecen en el color de fondo predeterminado. Podría usar el comando en cada fila, sin embargo, solo quiero tener un comando.

Gracias.

El MWE es:

\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}

ingrese la descripción de la imagen aquí

Respuesta1

Puede cargar xcolorcon la opción [table]en lugar de colortbl: extiende este último con un \rowcolors{no of 1st coloured row}{odd rows colour}{even rows colour}comando que hace lo que desea si elige el mismo color para las filas pares e impares.

Solo un comentario aparte: no creo que las reglas horizontales entre filas de colores se vean muy bien. También podrías reemplazarlos con \addlinespace. Además, si desea reglas horizontales sin franjas blancas, puede configurar \above/belowrulesepy 0ptagregar algo de relleno (de color) con el cellspacepaquete. Último punto: en mi opinión, los encabezados de columna blancos sobre un fondo negro son más legibles con una fuente en negrita.

\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} 

ingrese la descripción de la imagen aquí

Editar:

Otro código, jugando con los interruptores \showRowcolorsy \hideRowcolorspara activar y desactivar el coloreado de filas:

\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} 

ingrese la descripción de la imagen aquí

Respuesta2

No pondría líneas tabulares en un argumento. Esto es bastante frágil. Usaría una sintaxis como \startrelease...\stoprelease

Además de esto: si su \tableInternparte termina con una línea de comando diferente, entonces algo como esto podría funcionar:

\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}

Respuesta3

Una idea es crear una "columna extra fantasma" y dar un comando vacío de varias filas dentro de ella con solo el color de la fila... Si encuentras dificultades... pídeme que te ayude... (Lo intenté un poco pero no un poco complicado de introducir en tu código). Creo que puedes hacerlo muy fácil (Ya sabes cómo funciona tu código)

información relacionada