# Horizontal space between table and text

In this code

\documentclass[11pt]{article}

\usepackage{amsmath}
\usepackage[ngerman]{babel}
\usepackage{siunitx}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{graphicx}
\usepackage{lastpage}
\usepackage{xltabular,array}
\usepackage{showframe}

\sisetup{per-mode=fraction}

\pagestyle{empty}

\begin{document}

\vspace*{-2cm}

\noindent % it doesn't seem to make a difference

\begin{tabular}{|>{\raggedright\arraybackslash}m{3cm}|>{\raggedright\arraybackslash}m{7cm}|>{\raggedright\arraybackslash}m{5cm}|>{\raggedright\arraybackslash}m{2cm}|}
\hline
%\rule[-1ex]{0pt}{0.5ex}
\includegraphics[width=3cm]{example-image} &
\multicolumn{2}{|>{\raggedright\arraybackslash}m{12cm}|}{\rule[-1ex]{0pt}{4.5ex}{\LARGE \textbf{Mein-wunderschönes-Gymnasium}}} & \\
&
\multicolumn{2}{|>{\raggedright\arraybackslash}m{12cm}|}{{\rule[-2ex]{0pt}{6.5ex} \textbf{\Large Aufbau eines Versuchsprotokolls}}}  &
\rule[-2ex]{0pt}{6.5ex} {\Large \textbf{(WHO)}}  \\
\rule[-1ex]{0pt}{7.5ex} {\Large Klasse:} &
\rule[-1ex]{0pt}{7.5ex} {\Large Name:}  &
\rule[-1ex]{0pt}{7.5ex} {\Large Datum:}  &
\rule[-1ex]{0pt}{7.5ex} {\Large Seite \thepage\ von \pageref{LastPage}} \\
\hline
\end{tabular}

\section*{Section title begins here}

Text begins here.

\end{document}


I get a table that doesn't start flush left at the text margin. I added the frame to show what I mean. I get

I know I could pull the table to the left with a \hspace, but that would require trial and error. Is there a length I need to adjust to fix this automatically? (Nevermind that the table is also too wide: I will fix that as soon as I got it aligned properly).

• Try deleting the blank line between \noindent and \begin{tabular}. Jul 10 at 17:46
• @Bernard It worked, thank you. Do you want to make it an answer? Jul 10 at 18:18
• Thank you, but I don't think it's necessary. This is pretty standard and I'm sure there already exists such an answer (albeit I don't know where). Jul 10 at 18:27

Do you need a table at all?

\documentclass[11pt]{article}

\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{graphicx}
\usepackage{lastpage}
%\usepackage{showframe}

\pagestyle{empty}

\begin{document}

\vspace*{-2cm}
\noindent\begin{minipage}{\textwidth}
\hrule\smallskip

\LARGE

\makebox[0pt][l]{%
\begin{tabular}{@{}c@{}}\includegraphics[width=3cm]{example-image}\end{tabular}%
}\hfill
\textbf{Mein-wunderschönes-Gymnasium}\hspace*{\fill}

\Large

\hspace*{\fill}\textbf{Aufbau eines Versuchsprotokolls\hspace*{\fill}\makebox[0pt][r]{(WHO)}}

\bigskip

\makebox[0.2\textwidth][l]{Klasse:}%
\makebox[0.4\textwidth][l]{Name:}%
\makebox[0.2\textwidth][l]{Datum:}\hfill
\begin{tabular}{@{}l@{}}Seite \thepage\ \\ von \pageref{LastPage}\end{tabular}

\smallskip
\hrule
\end{minipage}

\section*{Section title begins here}

Text begins here.

\end{document}


Without showframe

You can even have the header larger than the text width:

\documentclass[11pt]{article}

\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{graphicx}
\usepackage{lastpage}
%\usepackage{showframe}

\pagestyle{empty}

\begin{document}

\vspace*{-2cm}
\noindent\hspace{-1cm}\begin{minipage}{\dimexpr\textwidth+2cm}
\hrule\smallskip

\LARGE

\makebox[0pt][l]{%
\begin{tabular}{@{}c@{}}\includegraphics[width=3cm]{example-image}\end{tabular}%
}\hfill
\textbf{Mein-wunderschönes-Gymnasium}\hspace*{\fill}

\Large

\hspace*{\fill}\textbf{Aufbau eines Versuchsprotokolls\hspace*{\fill}\makebox[0pt][r]{(WHO)}}

\bigskip

\makebox[0.2\textwidth][l]{Klasse:}%
\makebox[0.4\textwidth][l]{Name:}%
\makebox[0.2\textwidth][l]{Datum:}\hfill
\begin{tabular}{@{}l@{}}Seite \thepage\ \\ von \pageref{LastPage}\end{tabular}

\smallskip
\hrule
\end{minipage}\hspace*{-1cm}

\section*{Section title begins here}

Text begins here.

\end{document}


• That's obviously a possibility too. I feel that with a table all parts belong together as it should be, also from a logical point of view. Of course with your solution you have more flexibility in the case you do want to move things around. Any particular reason to put the picture in a table? Jul 11 at 6:00
• @Andyc The tabular around the picture is for vertical centering Jul 11 at 7:26

In addition to eliminating the blank line after \noindent, you may want to (a) make sure that the table actually fits inside the textblock and (b) declutter the code, possibly along the following lines.

\documentclass[11pt]{article}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{amsmath}
\usepackage[ngerman]{babel}
\usepackage{siunitx}
\usepackage{graphicx,lastpage,xltabular,array,calc}
\newcolumntype{M}[1]{>{\raggedright\arraybackslash}m{#1}}
%\usepackage{showframe}

\sisetup{per-mode=fraction}

\pagestyle{empty}

\begin{document}

\noindent % it makes a huge difference...
\begin{tabular}{@{}
M{3cm}
M{7cm-2\tabcolsep}
M{4.75cm-2\tabcolsep}
M{2.25cm-2\tabcolsep} @{}}
\hline
%\rule[-1ex]{0pt}{0.5ex}
\includegraphics[width=3cm-\tabcolsep]{example-image} &
\multicolumn{2}{c}{\LARGE\textbf{Mein wunderschönes Gymnasium}} & \\
&
\multicolumn{2}{c}{\rule[-2ex]{0pt}{6.5ex}\Large\textbf{Aufbau eines Versuchsprotokolls}}  &
\Large \textbf{(WHO)}  \\
\rule[-1ex]{0pt}{7.5ex}\Large Klasse: &
\rule[-1ex]{0pt}{7.5ex}\Large Name:  &
\rule[-1ex]{0pt}{7.5ex}\Large Datum:  &
\rule[-1ex]{0pt}{7.5ex}\Large Seite \thepage\ von \pageref{LastPage} \\
\hline
\end{tabular}

\section*{Section title begins here}

Text begins here.

\end{document}

• Thank you. Making it fit inside the textblock was indeed the next project, as I said in the question, and that's done. Decluttering the code is very nice indeed. Jul 10 at 20:07