Tabularx - ganze Zeilen einfärben

Tabellen und Grafiken erstellen und anordnen


Tim133
Forum-Newbie
Forum-Newbie
Beiträge: 1
Registriert: Fr 19. Jun 2020, 17:57

Tabularx - ganze Zeilen einfärben

Beitrag von Tim133 »

Guten Morgen liebes Latex Forum,

ich scheitere dabei einzelne Zeilen in einer meinen Tabellen einzufärben. Hier das Minimalbeispiel:

\documentclass[12pt,oneside,bibliography=totoc,parskip=off, appendixprefix=true, chapterprefix=false, numbers=noenddot]{scrbook}

\usepackage{tabularx}
\usepackage{colortbl}

\begin{document}
\begin{table}[ht]
	\centering
	\caption{Werkzeugstandmengen und -kosten}
	\begin{tabularx}{\textwidth}{@{\extracolsep{\fill}} lcccc}
		\toprule\begin{tabular}[c]{@{}c@{}}Technologie\end{tabular} 
		&\begin{tabular}[c]{@{}c@{}}Standlänge \\  $[\mathrm{mm}]$\end{tabular} 
		& \begin{tabular}[c]{@{}c@{}}Nachschliffint. \\$[\mathrm{Stk.}]$\end{tabular} 
		& \begin{tabular}[c]{@{}c@{}}Gesamtstückzahl\\ $[\mathrm{Stk.}]$\end{tabular} 
		& \begin{tabular}[c]{@{}c@{}}Werkzeugkosten\\ $[$\euro/$\mathrm{Stk.}]$\end{tabular}  \\
		\midrule
		\rowcolor[gray]{0.7}kon. deg. Z14& $3325$ & $133$& $4389$&$1,50$ \\
		\rowcolor[gray]{0.7}kon. deg. Z13& $3075$ &$123$& $4059$&$1,62$ \\
		\rowcolor[gray]{0.7}kon. deg. Z12& $2850$&$114$& $3762$&$1,75$ \\
		kon. deg. Z11& $2600$& $104$& $3432$&$1,92$\\
		\rowcolor[gray]{0.7}kon. deg. Z10& $2375$& $95$&$3135$&$2,10$ \\
		kon. deg. Z09& $2125$& $85$&$2805$&$2,35$ \\
		kon. deg. Z08& $1900$& $76$&$2508$&$2,63$ \\
		\rowcolor[gray]{0.7}zyl. deg. Z14& $3325$& $133$&$6650$&$1,32$\\
    	\rowcolor[gray]{0.7}zyl. deg. Z13& $3075$ & $123$& $6150$&$1,43$\\
		\rowcolor[gray]{0.7}zyl. deg. Z12& $2850$ &$114$&$5700$&$1,54$\\
		\rowcolor[gray]{0.7}zyl. deg. Z11& $2600$ &$104$&$5200$&$1,69$ \\
		\rowcolor[gray]{0.7}zyl. deg. Z10& $2375$ & $95$&$4750$&$1,85$ \\
		\rowcolor[gray]{0.7}zyl. deg. Z09& $2125$ & $85$&$4250$&$2,07$ \\
		\rowcolor[gray]{0.7}zyl. deg. Z08& $1900$ & $76$&$3800$&$2,32$\\
		\bottomrule  
\label{tab:Standmenge}
\end{tabularx} \end{table} \end{document}

ich hoffe mal das diese Pakete reichen. Ich hätte die Farbe gerne konstant über die ganze Zeile hinweg ohne Unterbrechung. Des Weiteren sollte die Farbe die Tabellenbreite nicht überschreiten. Ich vermute mal es hängt mit dem \extracolsep zusammen.

VG Tim


Stamm-Gast

Deklaration der Spalten anpassen

Beitrag von Stamm-Gast »

Das Beispiel ist nicht lauffähig, weil mindestens »booktabs« und »eurosym« fehlen. Ein vorangehender Test hätte das verhindert. Und um den Kopf der Tabelle so zu generieren braucht es keine Tabellen in der Tabelle, weil das mit »multirow« einfacher geht und zu dem übersichtlicher wird. Dafür muss aber eine zusätzliche Zeile für die Einheiten her (die hier fälschlicherweise in eckigen Klammern gesetzt wurden). Und »colortbl« lässt sich durch die Option 'table' beim Laden von »xcolor« einbinden.

Das eigentlche Problem ist aber die Spaltendeklaration für die Umgebung 'tabularx'. Hier werden keine Spaltentypen verwendet, die »tabularx« zur Verfügung stellt. Stattdessen werden die herkömmlichen Typen für 'tabular(*)' verwendet udn die tabelle dann mit '\extracolsep' aufgefüllt.

Das hier entspricht wohl mehr den Vorstellungen und beseitigt das Problem der über den Rand hinausragenden Färbung der Zeilen.

\documentclass[
  fontsize=12pt,
  oneside,
  bibliography=totoc,
  captions=tableabove,
%  parskip=off,
  appendixprefix=true,
  chapterprefix=false,
  numbers=noenddot,
  ngerman
]{scrbook}
\usepackage[T1]{fontenc}
\usepackage{babel}

\usepackage{booktabs,multirow,tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage[svgnames,table]{xcolor}

\usepackage{lmodern}
\usepackage{eurosym}
\usepackage{microtype}

\begin{document}
  \begin{table}[!ht]
    \centering
    \caption{Werkzeugstandmengen und -kosten}
    \label{tab:standmenge}  	          
    \begin{tabularx}{\textwidth}{lCCCC}\toprule
      \multirow{2}{*}{Technologie} & Standlänge & Nachschliffint.& Gesamtstückzahl &  Werkzeugkosten \\
                  &       (mm) &         (Stk.) &          (Stk.) & (\euro/Stk.) \\ \midrule
      \rowcolor[gray]{0.7}kon. deg. Z14& $3325$ & $133$& $4389$&$1,50$ \\
      \rowcolor[gray]{0.7}kon. deg. Z13& $3075$ &$123$& $4059$&$1,62$ \\
      \rowcolor[gray]{0.7}kon. deg. Z12& $2850$&$114$& $3762$&$1,75$ \\
      kon. deg. Z11& $2600$& $104$& $3432$&$1,92$\\
      \rowcolor[gray]{0.7}kon. deg. Z10& $2375$& $95$&$3135$&$2,10$ \\
      kon. deg. Z09& $2125$& $85$&$2805$&$2,35$ \\
      kon. deg. Z08& $1900$& $76$&$2508$&$2,63$ \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z14 & 3325 &  133 & 6650 & 1,32 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z13 &  3075 & 123 & 6150 & 1,43 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z12 & 2850 & 114 & 5700 & 1,54 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z11 & 2600 & 104 & 5200 & 1,69 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z10 & 2375 &  95 & 4750 & 1,85 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z09 & 2125 &  85 & 4250 & 2,07 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z08 & 1900 &  76 & 3800 & 2,32 \\ \bottomrule  
    \end{tabularx}
  \end{table}
\end{document}

Bartman
Forum-Meister
Forum-Meister
Beiträge: 2456
Registriert: Do 16. Jul 2009, 21:41
Wohnort: Hessische Provinz

Re: Tabularx - ganze Zeilen einfärben

Beitrag von Bartman »

@Tim133

Meine Änderung des Beispiels des Stammgastes lässt in einer Spalte den Spaltentyp S des Pakets siunitx mit dem Spaltentyp X zusammenarbeiten, um eine alternative Anordnung der Zahlen zu zeigen.

Der mathematische Modus wird in keiner Spalte gebraucht und sorgt in der letzten Spalte, wie zu erwarten, für einen Abstand nach dem Komma.

\documentclass[
  fontsize=12pt,
  oneside,
  bibliography=totoc,
  captions=tableabove,
%  parskip=off,
  appendixprefix=true,
  chapterprefix=false,
  numbers=noenddot,
  ngerman
]{scrbook}

\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{booktabs,tabularx}% Meine Änderung kommt ohne multirow aus.
\usepackage[svgnames,table]{xcolor}
\usepackage{lmodern}
\usepackage{eurosym}
\usepackage{microtype}
\usepackage[locale=DE]{siunitx}

\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
  \begin{table}[htbp]
    \centering
    \caption{Werkzeugstandmengen und -kosten}
    \label{tab:standmenge}
    \begin{tabularx}{\textwidth}{
      lC
      S[table-format=3]
      CC
    }
      \toprule
      Technologie & Standlänge & 
      \multicolumn{1}{>{\centering}X}{Nachschliffint.} &% oder
%      \multicolumn{1}{C}{Nachschliffint.} & 
      Gesamtstückzahl &  Werkzeugkosten \\
      & mm & {Stk.} & Stk. & \euro/Stk. \\
      \midrule
      \rowcolor[gray]{0.7}kon. deg. Z14& 3325 & 133 & 4389 & 1,50 \\
      \rowcolor[gray]{0.7}kon. deg. Z13& 3075 & 123 & 4059 & 1,62 \\
      \rowcolor[gray]{0.7}kon. deg. Z12& 2850 & 114 & 3762 & 1,75 \\
      kon. deg. Z11& 2600& 104& 3432&1,92\\
      \rowcolor[gray]{0.7}kon. deg. Z10& 2375& 95 &3135&2,10 \\
      kon. deg. Z09& 2125& 85 &2805&2,35 \\
      kon. deg. Z08& 1900& 76 &2508&2,63 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z14 & 3325 &  133 & 6650 & 1,32 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z13 &  3075 & 123 & 6150 & 1,43 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z12 & 2850 & 114 & 5700 & 1,54 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z11 & 2600 & 104 & 5200 & 1,69 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z10 & 2375 &  95 & 4750 & 1,85 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z09 & 2125 &  85 & 4250 & 2,07 \\
      \rowcolor[gray]{0.7}
      zyl. deg. Z08 & 1900 &  76 & 3800 & 2,32 \\ 
      \bottomrule
    \end{tabularx}
  \end{table}
\end{document}

Eine Variante mit \rowcolors:

\documentclass[
  fontsize=12pt,
  oneside,
  bibliography=totoc,
  captions=tableabove,
%  parskip=off,
  appendixprefix=true,
  chapterprefix=false,
  numbers=noenddot,
  ngerman
]{scrbook}

\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{booktabs,tabularx}% Meine Änderung kommt ohne multirow aus.
\usepackage[svgnames,table]{xcolor}
\usepackage{lmodern}
\usepackage{eurosym}
\usepackage{microtype}
\usepackage[locale=DE]{siunitx}

\newcolumntype{C}{>{\centering\arraybackslash}X}

% https://tex.stackexchange.com/questions/297345
\newcounter{tblerows}
\expandafter\let\csname c@tblerows\endcsname\rownum

\begin{document}
  \begin{table}[htbp]
    \centering
    \caption{Werkzeugstandmengen und -kosten}
    \label{tab:standmenge} 
    \rowcolors{3}{gray!70}{gray!70}
    \begin{tabularx}{\textwidth}{
      lC
      S[table-format=3]
      CC
    }
      \toprule
      Technologie & Standlänge & 
      \multicolumn{1}{>{\centering}X}{Nachschliffint.} &% oder
%      \multicolumn{1}{C}{Nachschliffint.} & 
      Gesamtstückzahl &  Werkzeugkosten \\
      & mm & {Stk.} & Stk. & \euro/Stk. \\
      \midrule
      kon. deg. Z14 & 3325 & 133 & 4389 & 1,50 \\
      kon. deg. Z13 & 3075 & 123 & 4059 & 1,62 \\
      kon. deg. Z12 & 2850 & 114 & 3762 & 1,75 \\
      \hiderowcolors
      kon. deg. Z11 & 2600 & 104 & 3432 & 1,92\\
      \showrowcolors
      kon. deg. Z10 & 2375 &  95 & 3135 & 2,10 \\
      \hiderowcolors
      kon. deg. Z09 & 2125 &  85 & 2805 & 2,35 \\
      kon. deg. Z08 & 1900 &  76 & 2508 & 2,63 \\
      \showrowcolors
      zyl. deg. Z14 & 3325 & 133 & 6650 & 1,32 \\
      zyl. deg. Z13 & 3075 & 123 & 6150 & 1,43 \\
      zyl. deg. Z12 & 2850 & 114 & 5700 & 1,54 \\
      zyl. deg. Z11 & 2600 & 104 & 5200 & 1,69 \\
      zyl. deg. Z10 & 2375 &  95 & 4750 & 1,85 \\
      zyl. deg. Z09 & 2125 &  85 & 4250 & 2,07 \\
      zyl. deg. Z08 & 1900 &  76 & 3800 & 2,32 \\ 
      \bottomrule
    \end{tabularx}
  \end{table}
\end{document}

Antworten