将表格列中的文本居中

将表格列中的文本居中

我不知道为什么我的表格的第二列和第三列的文本没有居中。我尝试使用\usepackage{array}with|>{\centering\arraybackslash}c|来放置这些列,但没有成功。在查看了 stack exchange 中的不同线程后,我没有找到解决方案 - 您能帮我吗?我还尝试使用tabular而不是tabular*,结果如下。这是输出,代码如下: 在此处输入图片描述

\documentclass[
12pt,                                                       % Schriftgröße
a4paper,                                                    % Layout für DINA4
german,                                                     % deutsche Sprache, global
twoside,                                                    % Layout für beidseitigen Druck
headinclude,                                            % Kopfzeile wird Seiten-Layouts mit berücksichtigt
headsepline,                                            % horizontale Linie unter Kolumnentitel
plainheadsepline,                                       % horizontale Linie unter Kolumnentitel auch bei Chapter
BCOR=20mm,                                              % Korrektur für die Bindung
DIV=18,                                                 % DIV-Wert für die Erstellung des Satzspiegels, siehe scrguide
parskip=half,                                           % Absatzabstand statt Absatzeinzug
openany,                                                    % Kapitel können auf geraden und ungeraden Seiten beginnen
bibliography=totoc,version=first,                       % Literaturverz. wird ins Inhaltsverzeichnis eingetragen
numbers=noenddot,                                   % Kapitelnummern immer ohne Punkt
captions=tableheading,version=first,                    % korrekte Abstände bei Tabellenüberschriften
fleqn,                                                  % fleqn: Glgen links (statt mittig)
listof=totoc,version=first,                             % Abbildungs- und Tabellenverzeichnis ins Inhaltsverzeichnis
cleardoublepage=empty                               % Kopfzeile bei leeren Seiten entfernen
]{scrbook}


%--------------- Packages ----------------
\usepackage[ngerman]{babel}                         % Neue deutsche Trennmuster
\usepackage[latin1]{inputenc}                           % direkte Eingabe von Umlauten & Co.
\usepackage[T1]{fontenc}                                % T1-Schriften
\usepackage{lmodern}                                % moderne Schriften
\usepackage[format=hang,                            % Captions ausrichten
justification=raggedright,
singlelinecheck=off,
labelfont=bf,
font=small,
skip=4pt
]{caption}                                          
\usepackage[centertags]{amsmath}                    % AMS-Mathematik, centertags zentriert Nummer bei split
\usepackage{tabularx}                               % erweiterte Tabellen
\usepackage{graphicx}                                       % zum Einbinden von Grafiken
\usepackage{setspace}                                       % Zeilenabstand einstellbar
\usepackage{scrlayer-scrpage}                               % Kopf- und Fußzeilen-Layout 
\usepackage[pdfborderstyle={/S/U/W 1}]{hyperref}        % Die Links im PDF werden nur noch dünn und rot unterstrichen, nicht mehr fett umrahmt. 
\usepackage[absolute]{textpos}                      % benutzerdefinierte Textpositionierung 
\usepackage{textgreek}                              % Greek letters in text without changing to math mode
\usepackage{amsmath}                                % Formeln nummerieren
\usepackage{pgfplots}                               % Visualization tool for creating scientific/technical graphics
\pgfplotsset{width=10cm,compat=1.9}             % changes the size of each pgfplot figure to 10 centimeters.  The compat parameter is for the code to work on the package version 1.9 or later.
\usepackage{microtype}                              % aktiviert das sogenannte HZ-Programm, mit dem bessere Umbruchpunkte gefunden werden können.
\usepackage{makecell}                               % manuelle Zeilenumbrüche in Tabellenzellen setzen
\usepackage{array}                                  % Zentrierte Spalten in Tabellen


%--------------- Sonstiges ----------------

\pagestyle{scrheadings}                                 % Kopf- und Fußzeile...
\renewcommand{\headfont}{\normalfont\sffamily}          % Kolumnentitel serifenlos
\renewcommand{\pnumfont}{\normalfont\sffamily}          % Seitennummern serifenlos
\ihead[]{\headmark}                                             % Kopfzeile innen
\ohead[\pagemark]{\pagemark}                                % Kopfzeile außen
\ifoot[]{}                                              % Fußzeile innen
\ofoot[]{}                                                  % Fußzeile außen    
\setlength{\headheight}{1.5\baselineskip}
\onehalfspacing                                         % 1,5 Zeilenabstand
%\typearea[current]{current}                                    % Neuberechnung des Satzspiegels mit alten Werten nach Änderung von Zeilenabstand,etc
\renewcommand{\bibname}{Literatur und Quellen}          % Literaturverzeichnisbezeichnung
\renewcommand{\figurename}{Abb.}                        % Abbildungsbezeichnung
\renewcommand{\listfigurename}{Abbildungsverzeichnis}   % Abbildungsverzeichnisbezeichnung
\renewcommand{\captionfont}{\small}                     % Bildunterschriften klein kursiv
\graphicspath{{figs/}{bilder/}}                             % Bildverzeichnis

\RedeclareSectionCommand[beforeskip=0pt]{chapter}       % kein Abstand zum oberen Seitenrand bei neuen Kapiteln

\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}} % Zentrierte Spalten in Tabellen


\makeindex

\begin{document}

\begin{table}[!htb]
    \centering
    \caption[test]{test} 
    \label{tab:tabelle1}
    \begin{tabular*}{\linewidth}{@{\extracolsep{\fill}} X|c|c|c|}
        \hline
        Production Type & Mean H [s] & Loading factor [-] \\ \hline
        Nuclear & 5,9 & 0,96 \\ \hline
        Fossil Brown coal/Lignite & 3,8 & 0,81 \\ \hline
        Fossil Peat & 3,8 & 0,59 \\ \hline
        Fossil Hard coal & 4,2 & 0,70 \\ \hline
        Fossil Gas & 4,2 & 0,60 \\ \hline
        Fossil Coal-derived gas & 4,2 & 0,54 \\ \hline
        Fossil Oil & 4,3 & 0,40 \\ \hline
        Fossil Oil shale & 4,3 & 0,40 \\ \hline
        Hydro Run-of-river and poundage & 2,7 & 0,61 \\ \hline
        Hydro Water Reservoir & 3,7 & 0,56 \\ \hline
        Hydro Pumped Storage & 3,5 & 0,46 \\ \hline
        Wind Onshore & 0 & - \\ \hline
        Wind Offshore & 0 & - \\ \hline
        Solar & 0 & - \\ \hline
        Other renewable & 3,5 & 0,50 \\ \hline
        Geothermal & 3,5 & 0,83 \\ \hline
        Other & 3,8 & 0,56 \\ \hline
        Waste & 3,8 & 0,28 \\ \hline
        Marine & 3,8 & 0,50 \\ \hline
        Biomass & 3,3 & 0,70 \\ \hline
    \end{tabular*}
\end{table}

\end{document}

带有表格的版本可以工作,但其宽度与文本不同:

\begin{table}[!htb]
    \centering
    \caption[test]{test} 
    \label{tab:tabelle1}
    \begin{tabular}{|c|c|c|}
        \hline
        Production Type & Mean H [s] & Loading factor [-] \\ \hline
        Nuclear & 5,9 & 0,96 \\ \hline
        Fossil Brown coal/Lignite & 3,8 & 0,81 \\ \hline
        Fossil Peat & 3,8 & 0,59 \\ \hline
        Fossil Hard coal & 4,2 & 0,70 \\ \hline
        Fossil Gas & 4,2 & 0,60 \\ \hline
        Fossil Coal-derived gas & 4,2 & 0,54 \\ \hline
        Fossil Oil & 4,3 & 0,40 \\ \hline
        Fossil Oil shale & 4,3 & 0,40 \\ \hline
        Hydro Run-of-river and poundage & 2,7 & 0,61 \\ \hline
        Hydro Water Reservoir & 3,7 & 0,56 \\ \hline
        Hydro Pumped Storage & 3,5 & 0,46 \\ \hline
        Wind Onshore & 0 & - \\ \hline
        Wind Offshore & 0 & - \\ \hline
        Solar & 0 & - \\ \hline
        Other renewable & 3,5 & 0,50 \\ \hline
        Geothermal & 3,5 & 0,83 \\ \hline
        Other & 3,8 & 0,56 \\ \hline
        Waste & 3,8 & 0,28 \\ \hline
        Marine & 3,8 & 0,50 \\ \hline
        Biomass & 3,3 & 0,70 \\ \hline
    \end{tabular}
\end{table}

在此处输入图片描述

我以为使用\begin{tabular}{\textwidth}{|c|c|c|}可以解决这个问题,但是它破坏了表格并出现几个错误:

83: Package array Error: Illegal pream-token (\textwidth): `c' used. \begin{tabular}{\textwidth}
84: Misplaced \noalign. \hline
84: You can't use `\hrule' here except with leaders. \hline
84: Missing number, treated as zero. \hline
84: Illegal unit of measure (pt inserted). \hline
85: Extra alignment tab has been changed to \cr. Production Type &
85: Extra alignment tab has been changed to \cr. Production Type & Mean H [s] &
86: Extra alignment tab has been changed to \cr. Nuclear &
86: Extra alignment tab has been changed to \cr. Nuclear & 5,9 &
87: Extra alignment tab has been changed to \cr. Fossil Brown coal/Lignite &
87: Extra alignment tab has been changed to \cr. Fossil Brown coal/Lignite & 3,8 &
88: Extra alignment tab has been changed to \cr. Fossil Peat &
88: Extra alignment tab has been changed to \cr. Fossil Peat & 3,8 &
89: Extra alignment tab has been changed to \cr. Fossil Hard coal &
89: Extra alignment tab has been changed to \cr. Fossil Hard coal & 4,2 &
90: Extra alignment tab has been changed to \cr. Fossil Gas &
90: Extra alignment tab has been changed to \cr. Fossil Gas & 4,2 &
91: Extra alignment tab has been changed to \cr. Fossil Coal-derived gas &
91: Extra alignment tab has been changed to \cr. Fossil Coal-derived gas & 4,2 &
92: Extra alignment tab has been changed to \cr. Fossil Oil &
92: Extra alignment tab has been changed to \cr. Fossil Oil & 4,3 &
93: Extra alignment tab has been changed to \cr. Fossil Oil shale &
93: Extra alignment tab has been changed to \cr. Fossil Oil shale & 4,3 &
94: Extra alignment tab has been changed to \cr. Hydro Run-of-river and poundage &
94: Extra alignment tab has been changed to \cr. Hydro Run-of-river and poundage & 2,7 &
95: Extra alignment tab has been changed to \cr. Hydro Water Reservoir &
95: Extra alignment tab has been changed to \cr. Hydro Water Reservoir & 3,7 &
96: Extra alignment tab has been changed to \cr. Hydro Pumped Storage &
96: Extra alignment tab has been changed to \cr. Hydro Pumped Storage & 3,5 &
97: Extra alignment tab has been changed to \cr. Wind Onshore &
97: Extra alignment tab has been changed to \cr. Wind Onshore & 0 &
98: Extra alignment tab has been changed to \cr. Wind Offshore &
98: Extra alignment tab has been changed to \cr. Wind Offshore & 0 &
99: Extra alignment tab has been changed to \cr. Solar &
99: Extra alignment tab has been changed to \cr. Solar & 0 &
100: Extra alignment tab has been changed to \cr. Other renewable &
100: Extra alignment tab has been changed to \cr. Other renewable & 3,5 &
101: Extra alignment tab has been changed to \cr. Geothermal &
101: Extra alignment tab has been changed to \cr. Geothermal & 3,5 &
102: Extra alignment tab has been changed to \cr. Other &
102: Extra alignment tab has been changed to \cr. Other & 3,8 &
103: Extra alignment tab has been changed to \cr. Waste &
103: Extra alignment tab has been changed to \cr. Waste & 3,8 &
104: Extra alignment tab has been changed to \cr. Marine &
104: Extra alignment tab has been changed to \cr. Marine & 3,8 &
105: Extra alignment tab has been changed to \cr. Biomass &
105: Extra alignment tab has been changed to \cr. Biomass & 3,3 &
: Switching back to an old KOMA-Script release does(scrbook) not mean, that KOMA-Script is fully compatible with(scrbook) the old release. If you need full compatibility(scrbook) to an old KOMA-Script release, you should use that(scrbook) old KOMA-Script release. However, in that case you(scrbook) could also need old releases of other packages and
: Switching back to an old KOMA-Script release does(scrbook) not mean, that KOMA-Script is fully compatible with(scrbook) the old release. If you need full compatibility(scrbook) to an old KOMA-Script release, you should use that(scrbook) old KOMA-Script release. However, in that case you(scrbook) could also need old releases of other packages and
: Switching back to an old KOMA-Script release does(scrbook) not mean, that KOMA-Script is fully compatible with(scrbook) the old release. If you need full compatibility(scrbook) to an old KOMA-Script release, you should use that(scrbook) old KOMA-Script release. However, in that case you(scrbook) could also need old releases of other packages and
: Switching back to an old KOMA-Script release does(typearea) not mean, that KOMA-Script is fully compatible with(typearea) the old release. If you need full compatibility(typearea) to an old KOMA-Script release, you should use that(typearea) old KOMA-Script release. However, in that case you(typearea) could also need old releases of other packages and
: Switching back to an old KOMA-Script release does(typearea) not mean, that KOMA-Script is fully compatible with(typearea) the old release. If you need full compatibility(typearea) to an old KOMA-Script release, you should use that(typearea) old KOMA-Script release. However, in that case you(typearea) could also need old releases of other packages and
: Switching back to an old KOMA-Script release does(typearea) not mean, that KOMA-Script is fully compatible with(typearea) the old release. If you need full compatibility(typearea) to an old KOMA-Script release, you should use that(typearea) old KOMA-Script release. However, in that case you(typearea) could also need old releases of other packages and
107: Float too large for page by 274.9989pt

答案1

我通过 ChatGPT 找到了解决方案。\usepackage{array}并且\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}在序言中是需要的。

\begin{document}
    
    \begin{table}[!htb]
        \centering
        \caption[test]{test} 
        \label{tab:tabelle1}
        \begin{tabularx}{\linewidth}{@{} |>{\centering\arraybackslash}X|>{\centering}X|>{\centering\arraybackslash}X|c @{}}
            \hline
            Production Type & Mean H [s] & Loading factor [-] \\ \hline
            Nuclear & 5,9 & 0,96 \\ \hline
            Fossil Brown coal/Lignite & 3,8 & 0,81 \\ \hline
            Fossil Peat & 3,8 & 0,59 \\ \hline
            Fossil Hard coal & 4,2 & 0,70 \\ \hline
            Fossil Gas & 4,2 & 0,60 \\ \hline
            Fossil Coal-derived gas & 4,2 & 0,54 \\ \hline
            Fossil Oil & 4,3 & 0,40 \\ \hline
            Fossil Oil shale & 4,3 & 0,40 \\ \hline
            Hydro Run-of-river and poundage & 2,7 & 0,61 \\ \hline
            Hydro Water Reservoir & 3,7 & 0,56 \\ \hline
            Hydro Pumped Storage & 3,5 & 0,46 \\ \hline
            Wind Onshore & 0 & - \\ \hline
            Wind Offshore & 0 & - \\ \hline
            Solar & 0 & - \\ \hline
            Other renewable & 3,5 & 0,50 \\ \hline
            Geothermal & 3,5 & 0,83 \\ \hline
            Other & 3,8 & 0,56 \\ \hline
            Waste & 3,8 & 0,28 \\ \hline
            Marine & 3,8 & 0,50 \\ \hline
            Biomass & 3,3 & 0,70 \\ \hline
        \end{tabularx}
    \end{table}
    
\end{document}

输出: 在此处输入图片描述

相关内容