Formatierung einer Tabelle

Tabellen und Grafiken erstellen und anordnen


Latex-Gast

Formatierung einer Tabelle

Beitrag von Latex-Gast »

Hallo allerseits,

ich habe seit wenigen Monaten längere Arbeiten mit Latex anzufertigen und muss sagen, ich bin weiterhin sehr glücklich damit. In den allermeisten Fällen findet man schnell Support und lösungen für die Probleme und es funktioniert ganz hervorragend. Ein Problem mit der Formatierung einer Tabelle habe ich jedoch bisher nicht lösen können.

Zwei kurze Worte zur Tabelle, es handelt sich um eine Aufstellung der Kosten eines Projektes, mit Zeilen für jede Aufwendung, eine Markierung für wann die Kosten eines Quartals anfallen und einigen selbst geschriebenen Funktionen die die einzelnen Punkte miteinander verknüpfen. Der Übersicht halber, habe ich die Tabelle mit einem dummy Text versehen und ein MWE geschrieben:
\documentclass[paper=a4, 
			fontsize=11pt, 
			headings=small, 
			DIV=12]{scrreprt}
\usepackage[utf8x]{inputenc}
\usepackage{lmodern,textcomp}
\usepackage[onehalfspacing]{setspace}

\usepackage{booktabs,tabularx,dcolumn, threeparttable, ragged2e}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}
\newcolumntype{R}{>{\RaggedLeft\arraybackslash}X}
\usepackage{multirow}
\usepackage{parskip}
\usepackage{hyperref}
\usepackage{multirow}
\usepackage{float}

\usepackage[table]{xcolor}
\definecolor{lightgrey}{RGB}{184,184,184}


\begin{document}
\begin{table}[!h]\onehalfspacing
\footnotesize
\begin{threeparttable}
\caption{Lorem ipsum dolor sit amet, consetetur sadipscing elitr}
\label{table:kostenplan}
\begin{tabularx}{1\textwidth}{@{} L R c c c c c c c c  R  @{}} \toprule
	Kostenpunkt & Kosten & \multicolumn{4}{>{\hsize=4\hsize}c}{\parbox{3cm}{\centering Jahr 1}}	&\multicolumn{4}{>{\hsize=4\hsize}c}{\parbox{3cm}{\centering Jahr 2}} & Summe \\
	\cmidrule(l){3-6}\cmidrule(l){7-10}
	& &Q1 &Q2 &Q3 &Q4 &Q1 &Q2 &Q3 &Q4 & \\
	\midrule
	Lorem ipsum dolor sit amet, consetetur sadipscing  & 99999 p. Qu. & \cellcolor{lightgrey}\textbf{$\times$} & \cellcolor{lightgrey}\textbf{$\times$} & \cellcolor{lightgrey}\textbf{$\times$} & \cellcolor{lightgrey}\textbf{$\times$} & \cellcolor{lightgrey}\textbf{$\times$} & \cellcolor{lightgrey}\textbf{$\times$} & \cellcolor{lightgrey}\textbf{$\times$} & \cellcolor{lightgrey}\textbf{$\times$} & 99999€\\ 
	Lorem ipsum dolor 1 & 33433€ p. Qu.& & &\cellcolor{lightgrey}\textbf{$\times$} &\cellcolor{lightgrey}\textbf{$\times$} & & & & & 33433€ \\ 
	Lorem ipsum dolor 1 & 22322€ p. Qu.& & & & \cellcolor{lightgrey}\textbf{$\times$} &\cellcolor{lightgrey}\textbf{$\times$} & & & & 23322€ \\
	Lorem ipsum dolor 1 & 11211€ p. Qu.& & & & & \cellcolor{lightgrey}\textbf{$\times$} & \cellcolor{lightgrey}\textbf{$\times$}  & & & 11211 € \\ 
	Lorem ipsum & \textit{einm} & \cellcolor{lightgrey}\textbf{$\times$} & & & & & & & & 56789 € \\ 
	Lorem ipsum & \textit{einm.} &\cellcolor{lightgrey}\textbf{$\times$} & & & & & & & &  4565 € \\ 
	Lorem & \textit{einm.} & \cellcolor{lightgrey}\textbf{$\times$} & & & & &  & & &  2345 € \\ 
	Lorem ipsum & & & & & & & & & \cellcolor{lightgrey}\textbf{$\times$} &  1212€\\ 
	Lorem ipsum dolor & & & & & & & & & \cellcolor{lightgrey}\textbf{$\times$} & 1111€ \\ 	
\bottomrule
 & & & & & & & & & \textit{Gesamt} & 1234567 € \\

\end{tabularx}
\end{threeparttable}
\end{table}

\end{document}
An und für sich ist alles da, aber ich würde gerne wissen, ob mir jedmand bei folgenden Fragen helfen kann:
1. wie stelle ich die Breite der Kopfzeile ein, dass bspw. Q1-Q4 jeweils identisch ist und die zweie und letzte Spalte an den Inhalt angepasst ist. Der Rest soll für die erste Spalte übrig bleiben.
2.Wenn bei der ersten Zeile ein Umbruch erfolgt, wie stellt man den abstand ein, dass dieser kleiner ist?
3. wie kann man den Text innerhalb der grauen Zellen zentreieren, wenn es einen Zeilenumbruch gibt? Also bspw. über >>Gesamt<<

Wäre für eine Antwort dankbar.
Viele Grüße, David

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

Beitrag von Bartman »

Ein paar Änderungsvorschläge für Deine Tabelle:
\documentclass[
%    paper=a4,% Voreinstellung
%    fontsize=11pt,% s.o.
    headings=small,
    DIV=12,
    parskip,% Ersatz für Paket parskip
    captions=tableheading% <- eingefügt
]{scrreprt}

% https://tex.stackexchange.com/questions/13067
\usepackage[utf8]{inputenc}% Option geändert; wird bei einer aktuellen TeX-Distribution nicht mehr benötigt
\usepackage[T1]{fontenc}% https://texwelt.de/fragen/5537
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{textcomp}% bietet \texttimes an
\usepackage[onehalfspacing]{setspace}
\usepackage{booktabs,tabularx,threeparttable,ragged2e}
\usepackage{multirow}% einmal laden reicht
%\usepackage{float}
\usepackage[table]{xcolor}
\usepackage[right]{eurosym}% für \euro und \EUR
\usepackage[locale=DE]{siunitx}
\usepackage{showframe}
\usepackage{hyperref}% in den meisten Fällen besser als letztes Paket laden

\newcolumntype{L}{>{\RaggedRight}X}% \arraybackslash entfernt
\newcolumntype{R}{>{\RaggedLeft}X}% s.o.

\definecolor{lightgrey}{RGB}{184,184,184}

\newcommand{\cross}{\textbf{\texttimes}}

\begin{document}
\begin{table}[!h]
\onehalfspacing
\footnotesize
%\begin{threeparttable}
\caption{Lorem ipsum dolor sit amet, consetetur sadipscing elitr}
\label{tab:kostenplan}
\begin{tabularx}{\textwidth}{@{} L R *8c S[table-format=7] @{}} \toprule
   Kostenpunkt & Kosten & \multicolumn{4}{c}{Jahr 1}   &\multicolumn{4}{c}{Jahr 2} & {Summe} \\
   \cmidrule(l){3-6}\cmidrule(l){7-10}
   & &Q1 &Q2 &Q3 &Q4 &Q1 &Q2 &Q3 &Q4 & \\
   & &   &   &   &   &   &   &   &   & \euro\\
   \midrule
   Lorem ipsum dolor sit amet, consetetur sadipscing  & 99999 p. Qu. & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & 99999\\
   Lorem ipsum dolor 1 & \EUR{33433} p. Qu.& & &\cellcolor{lightgrey}\cross &\cellcolor{lightgrey}\cross & & & & & 33433 \\
   Lorem ipsum dolor 1 & \EUR{22322} p. Qu.& & & & \cellcolor{lightgrey}\cross &\cellcolor{lightgrey}\cross & & & & 23322 \\
   Lorem ipsum dolor 1 & \EUR{11211} p. Qu.& & & & & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross  & & & 11211 \\
   Lorem ipsum & \textit{einm} & \cellcolor{lightgrey}\cross & & & & & & & & 56789 \\
   Lorem ipsum & \textit{einm.} &\cellcolor{lightgrey}\cross & & & & & & & &  4565 \\
   Lorem & \textit{einm.} & \cellcolor{lightgrey}\cross & & & & &  & & &  2345 \\
   Lorem ipsum & & & & & & & & & \cellcolor{lightgrey}\cross &  1212\\
   Lorem ipsum dolor & & & & & & & & & \cellcolor{lightgrey}\cross & 1111\\    
\midrule
 & & & & & & & & \multicolumn{2}{r}{\textit{Gesamt}} & 1234567 \\
\bottomrule
\end{tabularx}
%\end{threeparttable}
\end{table}
\end{document}
Latex-Gast hat geschrieben:3. wie kann man den Text innerhalb der grauen Zellen zentreieren, wenn es einen Zeilenumbruch gibt? Also bspw. über >>Gesamt<<
Nur in den grauen Zellen oder in der ganzen Zeile, wie bei
\renewcommand{\tabularxcolumn}[1]{m{#1}}
aus der Dokumentation von tabularx?

Latex-Gast

Beitrag von Latex-Gast »

Vielen Dank für das Optimieren des Codes und für die Beantwortung meiner Fragen. Und den Teil der Dokumentation von tabularx hatte ich nicht gesehen. Auch das ist was ich brauche.

Eine Sache ist mir noch nicht ganz klar. Diese Formulierung:
\begin{tabularx}{\textwidth}{@{} L R *8c S[table-format=7] @{}} \toprule
kannte ich nicht, führt aber wenn ich das richtig verstehe zu einer automatischen Definition der Breite nach dem siunit-package, also in diesem Fall sieben Stellen vor dem Komma. Ich bin ein wenig verwundert, wie es mit den anderen Spalten umgeht. Will ich zum Beispiel die letzte Zeile anders anordnen will, z.B.:
\begin{tabularx}{\textwidth}{@{} L R *8c S[table-format=7] R @{}} \toprule
passiert nichts. Oder was ist wenn ich einzelne Spalten wie die erste manuell größer setzen will? Verstehe glaube ich nicht so ganz, was das genau steuert.

Und noch eine Kleinigkeit zum Schluss. Kann man bei einem Zeilenumbruch innerhalb einer Zelle den Zeilenabstand ändern? Bin zwar nicht sicher, ob das in diesem Fall überhaupt sinnvoll ist, aber es gibt Fälle, in denen das in meinen Augen dadurch einfacher lesbar sein könnte.

Viele Grüße,
David

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

Beitrag von Bartman »

Latex-Gast hat geschrieben:Will ich zum Beispiel die letzte Zeile anders anordnen will, z.B.:
\begin{tabularx}{\textwidth}{@{} L R *8c S[table-format=7] R @{}} \toprule
passiert nichts.
Wenn Du am Ende der ersten Zeile mit dem Et-Zeichen eine weitere Zelle für die neue letzte Spalte einfügst, dann siehst Du die Veränderung.
Latex-Gast hat geschrieben:Oder was ist wenn ich einzelne Spalten wie die erste manuell größer setzen will? Verstehe glaube ich nicht so ganz, was das genau steuert.
Du kannst hier statt der X-Spalte auch einen der Spaltentypen p oder m nehmen und über deren erforderliches Argument eine bestimmte Länge angeben. Die LaTeX-Einführungen dürften diese Spaltentypen behandeln.
Latex-Gast hat geschrieben:Und noch eine Kleinigkeit zum Schluss. Kann man bei einem Zeilenumbruch innerhalb einer Zelle den Zeilenabstand ändern? Bin zwar nicht sicher, ob das in diesem Fall überhaupt sinnvoll ist, aber es gibt Fälle, in denen das in meinen Augen dadurch einfacher lesbar sein könnte.
Ein merkwürdiger Wunsch, der sich mit dem Befehl \setstretch des Pakets setspace in der gewünschten Zelle verwirklichen lässt.

Gast

Beitrag von Gast »

Bartman hat geschrieben:Ein merkwürdiger Wunsch, der sich mit dem Befehl \setstretch des Pakets setspace in der gewünschten Zelle verwirklichen lässt.
Oder ggf. auch mit \linespread, dann aber das notwendig \selectfont nicht vergessen.

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

Beitrag von Bartman »

Dank Raaja und egreg entspricht die Ausgabe der übrigen Euro-Beträge nun der in der Summenspalte.
\documentclass[
%    paper=a4,% Voreinstellung
%    fontsize=11pt,% s.o.
    headings=small,
    DIV=12,
    parskip,% Ersatz für Paket parskip
    captions=tableheading% <- eingefügt
]{scrreprt}

% https://tex.stackexchange.com/questions/13067
\usepackage[utf8]{inputenc}% Option geändert; wird bei einer aktuellen TeX-Distribution nicht mehr benötigt
\usepackage[T1]{fontenc}% https://texwelt.de/fragen/5537
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{textcomp}% bietet \texttimes an
\usepackage[onehalfspacing]{setspace}
\usepackage{booktabs,tabularx,threeparttable,ragged2e}
\usepackage{multirow}
%\usepackage{float}
\usepackage[table]{xcolor}
\usepackage[right]{eurosym}% für \euro und \EUR
\usepackage[locale=DE]{siunitx}
\usepackage{showframe}
\usepackage{hyperref}

\newcolumntype{L}{>{\RaggedRight}X}% \arraybackslash entfernt
\newcolumntype{R}{>{\RaggedLeft}X}% s.o.

\definecolor{lightgrey}{RGB}{184,184,184}

\newcommand{\eur}[1]{\EUR{\num{#1}}}
\newcommand{\cross}{\textbf{\texttimes}}

\begin{document}
\begin{table}[!h]
\onehalfspacing
\footnotesize
%\begin{threeparttable}
\caption{Lorem ipsum dolor sit amet, consetetur sadipscing elitr}
\label{tab:kostenplan}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\begin{tabularx}{\textwidth}{@{} L R *8c S[table-format=7] @{}} \toprule
   Kostenpunkt & Kosten & \multicolumn{4}{c}{Jahr 1}   &\multicolumn{4}{c}{Jahr 2} & {Summe}\\
   \cmidrule(l){3-6}\cmidrule(l){7-10}
   & &Q1 &Q2 &Q3 &Q4 &Q1 &Q2 &Q3 &Q4 & \\
   & &   &   &   &   &   &   &   &   & \euro\\
   \midrule
    Lorem ipsum dolor sit amet, consetetur sadipscing  & 99999 p. Qu. & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross & 99999\\
   Lorem ipsum dolor 1 & \eur{33433} p. Qu.& & &\cellcolor{lightgrey}\cross &\cellcolor{lightgrey}\cross & & & & & 33433 \\
   Lorem ipsum dolor 1 & \eur{22322} p. Qu.& & & & \cellcolor{lightgrey}\cross &\cellcolor{lightgrey}\cross & & & & 23322 \\
   Lorem ipsum dolor 1 & \eur{11211} p. Qu.& & & & & \cellcolor{lightgrey}\cross & \cellcolor{lightgrey}\cross  & & & 11211 \\
   Lorem ipsum & \textit{einm} & \cellcolor{lightgrey}\cross & & & & & & & & 56789 \\
   Lorem ipsum & \textit{einm.} &\cellcolor{lightgrey}\cross & & & & & & & &  4565 \\
   Lorem & \textit{einm.} & \cellcolor{lightgrey}\cross & & & & &  & & &  2345 \\
   Lorem ipsum & & & & & & & & & \cellcolor{lightgrey}\cross &  1212\\
   Lorem ipsum dolor & & & & & & & & & \cellcolor{lightgrey}\cross & 1111\\    
\midrule
 & & & & & & & & \multicolumn{2}{r}{\textit{Gesamt}} & 1234567 \\
\bottomrule
\end{tabularx}
%\end{threeparttable}
\end{table}
\end{document}

Antworten