2

I'm trying to create a table using custom columns as explained here. For some reason in the last row of my table the very left column is not centered horizontally and I don't know why.

Here is the code im using:

\documentclass[
                german,
                paper=a4,
                oneside,
                DIV=15,
                12pt,
                parskip,
                headsepline,
                cleardoublepage=empty,
                ]{scrreprt}             

\usepackage[automark, autooneside=false]{scrlayer-scrpage}
\clearpairofpagestyles
\usepackage[latin1]{inputenc}
\usepackage[english,ngerman]{babel}
\usepackage[T1]{fontenc}    
\usepackage{lmodern}
\parindent 0pt
\usepackage[onehalfspacing]{setspace}
\usepackage[decimalsymbol=comma,
            loctolang={DE:ngerman,UK:english},
            ]{siunitx}
\usepackage{wg_makro}
\usepackage{ wasysym }
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk]{0.43, 0.35, 0.35,0.01}{\parbox{\textwidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}

%***************************************************************
%this is where I defined the new columntypes
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
%***************************************************************
\usepackage{chemmacros}

\begin{document}

\begin{center}
\begin{table}[H]
    \begin{tabular}{|M{3cm}|M{2.5cm}|M{2.3cm}|M{2.2cm}|M{2.5cm}|M{3.25cm}|}
        \hline
        Substrat & Abmessungen & Leitfähigkeit & Orientierung & Dotierung & Brechungsindex \newline $( \lambda = \SI{632,8}{\nano\meter})$ \\   \hline\hline
        p-Si & \diameter: \SI{2}{"} \newline H: \SI{1}{\milli\meter} & \SI{0.1}{\ohm\meter} & <100> & Bor-Dotiert & \num{1,5667-i0,016}\\ \hline
        n-Si & \diameter: \SI{2}{"} \newline H: \SI{1}{\milli\meter} & \SI{0.1}{\ohm\meter} & <100> & Bor-Dotiert & \num{1,5667-i0,016}\\ \hline
        p-Si \newline \ch{SiO2} (\SI{200}{\nano\meter}) & \diameter: \SI{2}{"} \newline H: \SI{1}{\milli\meter} & \SI{0.1}{\ohm\meter} & <100> & Bor-Dotiert & \num{1,5667-i0,016}\\ \hline
    \end{tabular}
    \caption{Haupteigenschaften der verwendeten Substrate}
    \label{tbl:substrate}
\end{table}
\end{center}

\end{document}

This is what I get as an result:

Not centered Cell

I added a red line so one can immediately see the wrong alignment.

So my question is: How do I align the last "p-Si" with the ones in the cells above?

Jamie-505
  • 155
  • Replace \newline with \endgraf. – Bernard Jul 27 '15 at 16:47
  • You could have pinged @Bernard so he can make up an answer, or find a corresponding duplicate. – Johannes_B Jul 27 '15 at 18:39
  • @ Johannes_B: That's unimportant. After all, @Jamie decided to draw the O.P.'s attention on my comment. – Bernard Jul 27 '15 at 18:46
  • @Bernard do you know why the problem only occurs in the first column but not in the other columns? – cgnieder Sep 15 '15 at 07:49
  • @clemens: Not the least idea. I'm not a TeX guru. Using \endgraf instead of \par, \newline, &c., is a trick that works sometimes, and that I try, just in case. This time, it worked. – Bernard Sep 15 '15 at 08:43

2 Answers2

1

You want neither \newline (never use it in tables) nor \endgraf.

The command \endgraf should never be used directly in a document; it may be handy for some special situations when macro definitions are involved. Well, “never” might be an exaggeration, but the idea is that if you seem to need it in a document, there's probably something wrong or, at least, disputable in what you're doing.

Here's a way that avoids computing column widths and M columns; the adjustment to \tabcolsep should be done at the very end, when the document is in final form; I just took the amount of overfull and divided it by 12 (the number of intercolumn spaces).

\documentclass[
  paper=a4,
  oneside,
  DIV=15,
  12pt,
  parskip,
  headsepline,
  cleardoublepage=empty,
]{scrreprt}             

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{wasysym}

\usepackage[onehalfspacing]{setspace}

\usepackage[decimalsymbol=comma,
            loctolang={DE:ngerman,UK:english},
            ]{siunitx}
\usepackage{chemmacros}

\DeclareSIUnit{\inch}{\ensuremath{''}}
\newcommand{\splitcell}[2][c]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}
\newcommand{\orientation}[1]{\ensuremath{\langle#1\rangle}}

\begin{document}

\begin{table}
\centering

\addtolength{\tabcolsep}{-0.74pt}% avoid overfull
\begin{tabular}{|*{6}{c|}}
\hline
Substrat & Abmessungen & Leitfähigkeit & Orientierung & Dotierung & 
  \splitcell{Brechungsindex \\ $(\lambda = \SI{632,8}{\nano\meter})$} \\
\hline\hline
p-Si & \splitcell[l]{\diameter: \SI{2}{\inch} \\ H: \SI{1}{\milli\meter}} &
  \SI{0.1}{\ohm\meter} & \orientation{100} & Bor-Dotiert & \num{1,5667-i0,016}\\
\hline
n-Si & \splitcell[l]{\diameter: \SI{2}{\inch} \\ H: \SI{1}{\milli\meter}} &
  \SI{0.1}{\ohm\meter} & \orientation{100} & Bor-Dotiert & \num{1,5667-i0,016}\\
\hline
\splitcell{p-Si \\ \ch{SiO2} (\SI{200}{\nano\meter})}
     & \splitcell[l]{\diameter: \SI{2}{\inch} \\ H: \SI{1}{\milli\meter}} &
  \SI{0.1}{\ohm\meter} & \orientation{100} & Bor-Dotiert & \num{1,5667-i0,016}\\
\hline
\end{tabular}

\caption{Haupteigenschaften der verwendeten Substrate}
\label{tbl:substrate}

\end{table}

\end{document}

I've removed the packages unnecessary for building the example.

Note that a table environment must not go inside a center environment, even if you use the [H] specifier (don't).

enter image description here

The same with booktabs (requires \usepackage{booktabs}):

\begin{table}
\centering

\addtolength{\tabcolsep}{-0.5pt}% avoid overfull
\begin{tabular}{*{6}{c}}
\toprule
Substrat & Abmessungen & Leitfähigkeit & Orientierung & Dotierung &
  \splitcell{Brechungsindex \\ $(\lambda = \SI{632,8}{\nano\meter})$} \\
\midrule
p-Si & \splitcell[l]{\diameter: \SI{2}{\inch} \\ H: \SI{1}{\milli\meter}} &
  \SI{0.1}{\ohm\meter} & \orientation{100} & Bor-Dotiert & \num{1,5667-i0,016}\\
\addlinespace
n-Si & \splitcell[l]{\diameter: \SI{2}{\inch} \\ H: \SI{1}{\milli\meter}} &
  \SI{0.1}{\ohm\meter} & \orientation{100} & Bor-Dotiert & \num{1,5667-i0,016}\\
\addlinespace
\splitcell{p-Si \\ \ch{SiO2} (\SI{200}{\nano\meter})}
     & \splitcell[l]{\diameter: \SI{2}{\inch} \\ H: \SI{1}{\milli\meter}} &
  \SI{0.1}{\ohm\meter} & \orientation{100} & Bor-Dotiert & \num{1,5667-i0,016}\\
\bottomrule
\end{tabular}

\caption{Haupteigenschaften der verwendeten Substrate}
\label{tbl:substrate}

\end{table}

enter image description here

egreg
  • 1,121,712
0

Turning @Bernhard's comment into an answer:

By replacing \newline with \endgraf, the alignment got fixed.

Johannes_B
  • 24,235
  • 10
  • 93
  • 248
Jamie-505
  • 155