![创建表设计](https://linux22.com/image/450966/%E5%88%9B%E5%BB%BA%E8%A1%A8%E8%AE%BE%E8%AE%A1.png)
我有一个问题。我想在 Latex 中创建一个表格。标题列(第一行)应居中。之后,下一个文本应左对齐并从顶部开始,因此它不应居中对齐,而应在顶部对齐。此外,我希望,例如,如果我有一些较长的文本,它应该自动有换行符,因为我有 tabular x。但是,整个过程对我来说不起作用,第一行缺少行。
\documentclass[12pt]{report}
\usepackage[usegeometry]{typearea}% load before geometry
\usepackage[a4paper, left={2.5cm}, right={2cm}, top={3cm}, bottom={3cm}, headheight=15pt]{geometry}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}} % for vertical centering text in X column
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}} % for vertical centering text in X column
\usepackage{caption}
\captionsetup[table]{position=bottom}
\usepackage{fancyhdr}
\fancypagestyle{plain}{%
\fancyhf{}%
\fancyhead[L]{\nouppercase{\leftmark}}%
\fancyfoot[R]{\thepage}}
\pagestyle{plain}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter. #1}{}}
\usepackage[parfill]{parskip}
\usepackage{blindtext}% only for dummy text
\begin{document}
\blinddocument
\clearpage
\KOMAoptions{paper=landscape,DIV=last}
%\newgeometry{margin=3cm}
\newgeometry{hmargin=2.5cm,bottom=25mm,height=150mm,includehead}
\fancyheadoffset{0pt}% recalculate headwidth for fancyhdr
\chapter{CRIPS-DM}
\setlength{\tabcolsep}{0.5em} % for the horizontal padding
{\renewcommand{\arraystretch}{1.2}% for the vertical padding
\begin{table}[H]
\caption[Name]{Überblick der Phasen des CRISP-DM Prozesses~\cite{PeterChapmanandJanetClintonandRandyKerberandTomKhabazaandThomasP.ReinartzandColinShe.2000}}
\label{tab:crisp__ueberblick}
\begin{tabularx}{\textwidth}{|X|X|X|X|X|X|}
\multicolumn{1}{|c|}{\textbf{BusinessUnderstanding}} & \multicolumn{1}{c|}{\textbf{Data Understanding}} & \multicolumn{1}{c|}{\textbf{Data Prepartion}} & \multicolumn{1}{c|}{\textbf{Modeling}} & \multicolumn{1}{c|}{\textbf{Evaluation}} & \multicolumn{1}{c|}{\textbf{Deployment}}
\\ \hline
This is a long Text \newline
What about a new line
& This is another long text what a long text is that \newline And agian an new line
& sad
& asd
& aa
& adasd
\\ \hline
\end{tabularx}
\end{table}
\clearpage
\KOMAoptions{paper=portrait,DIV=last}
\restoregeometry
\fancyheadoffset{0pt}
\blinddocument
\end{document}
答案1
正如我所说,删除命令The command \renewcommand\tabularxcolumn[1]{m{#1}}
会使文本在顶部对齐。要添加规则,您需要一个\hline
命令。
\documentclass[12pt]{report}
\usepackage[usegeometry]{typearea}% load before geometry
\usepackage[a4paper, left={2.5cm}, right={2cm}, top={3cm}, bottom={3cm}, headheight=15pt]{geometry}
\usepackage{tabularx}
% \renewcommand\tabularxcolumn[1]{m{#1}} % for vertical centering text in X column %-----> commented
\usepackage{caption}
\captionsetup[table]{position=bottom}
\usepackage{fancyhdr}
\fancypagestyle{plain}{%
\fancyhf{}%
\fancyhead[L]{\nouppercase{\leftmark}}%
\fancyfoot[R]{\thepage}}
\pagestyle{plain}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter. #1}{}}
\usepackage[parfill]{parskip}
\usepackage{float} %----> added
\begin{document}
\clearpage
\KOMAoptions{paper=landscape,DIV=last}
%\newgeometry{margin=3cm}
\newgeometry{hmargin=2.5cm,bottom=25mm,height=150mm,includehead}
\fancyheadoffset{0pt}% recalculate headwidth for fancyhdr
\chapter{CRIPS-DM}
\setlength{\tabcolsep}{0.5em} % for the horizontal padding
{\renewcommand{\arraystretch}{1.2}% for the vertical padding
\begin{table}[H]
\caption[Name]{Überblick der Phasen des CRISP-DM Prozesses~\cite{PeterChapmanandJanetClintonandRandyKerberandTomKhabazaandThomasP.ReinartzandColinShe.2000}}
\label{tab:crisp__ueberblick}
\begin{tabularx}{\textwidth}{|X|X|X|X|X|X|}
\hline %-----> added
\multicolumn{1}{|c|}{\textbf{BusinessUnderstanding}} & \multicolumn{1}{c|}{\textbf{Data Understanding}} & \multicolumn{1}{c|}{\textbf{Data Prepartion}} & \multicolumn{1}{c|}{\textbf{Modeling}} & \multicolumn{1}{c|}{\textbf{Evaluation}} & \multicolumn{1}{c|}{\textbf{Deployment}}
\\ \hline
This is a long Text \newline
What about a new line
& This is another long text what a long text is that \newline And agian an new line
& sad
& asd
& aa
& adasd
\\ \hline
\end{tabularx}
\end{table}
} %---> added
\clearpage
\KOMAoptions{paper=portrait,DIV=last}
\restoregeometry
\fancyheadoffset{0pt}
\end{document}
不过,我建议表格中水平线较少,垂直线较少。
\documentclass[12pt]{report}
\usepackage[usegeometry]{typearea}
\usepackage{geometry}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\KOMAoptions{paper=landscape,DIV=last}
\newgeometry{hmargin=2.5cm,bottom=25mm,height=150mm,includehead}
\begin{table}
\begin{tblr}{
colspec = {X[1.5]*{5}{X}}, %1st column width is 1.5 times of other columns
row{1} = {font=\bfseries, c, m} %makes 1st row text bold and centers it horizontally and vertically
}
\toprule
BusinessUnderstanding & Data Understanding & Data Prepartion & Modeling & Evaluation & Deployment
\\\midrule
This is a long Text \newline What about a new line
& This is another long text what a long text is that \newline And agian an new line
& sad
& asd
& aa
& adasd
\\
This is a long Text \newline
What about a new line
& This is another long text what a long text is that \newline And agian an new line
& sad
& asd
& aa
& adasd
\\\bottomrule
\end{tblr}
\caption[Name]{Überblick der Phasen des CRISP-DM Prozesses}
\label{tab:crisp__ueberblick}
\end{table}
\end{document}
在这里,我使用了tabularray
包使得管理表变得非常方便。
答案2
您使用的是tabularx
环境,但所有六个X
列定义都被指令覆盖\multicolumn{1}{c}{...}
,指令使用了c
列类型,而列类型不允许自动换行。简而言之,您没有让它tabularx
完成其工作。在下面的解决方案中,我建议改用C
列类型,其中C
定义为的居中/粗体版本X
。
一个单独的问题:摆脱该\renewcommand\tabularxcolumn[1]{m{#1}}
指令,因为 - 您在评论中注意到的一个事实...... - 它用于垂直居中列内容,这是您不想要的。
\documentclass[12pt]{report}
\usepackage[usegeometry]{typearea}% load before geometry
\usepackage[a4paper, left=2.5cm,right=2cm,vmargin=3cm, headheight=15pt]{geometry}
\usepackage{tabularx,ragged2e}
\newcolumntype{L}{>{\RaggedRight}X}
\newcolumntype{C}{>{\centering\bfseries}X}
\newcommand\mC[1]{\multicolumn{1}{C}{#1}} % handy shortcut macro
\usepackage[skip=0.333\baselineskip]{caption}
%\captionsetup[table]{position=bottom}
\usepackage{fancyhdr}
\fancypagestyle{plain}{%
\fancyhf{}%
\fancyhead[L]{\nouppercase{\leftmark}}%
\fancyfoot[R]{\thepage}}
\pagestyle{plain}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter. #1}{}}
\usepackage[parfill]{parskip}
\begin{document}
\KOMAoptions{paper=landscape,DIV=last}
%\newgeometry{margin=3cm}
\newgeometry{hmargin=2.5cm,bottom=25mm,height=150mm,includehead}
\fancyheadoffset{0pt}% recalculate headwidth for fancyhdr
\chapter{CRIPS-DM}
\setlength{\tabcolsep}{0.5em} % for horizontal padding
%%\renewcommand{\arraystretch}{1.2}% for the vertical padding
\setlength\extrarowheight{2pt}
\begin{table}[ht]
\caption[Name]{Überblick der Phasen des CRISP-DM Prozesses~\cite{xyz}}
\label{tab:crisp__ueberblick}
\begin{tabularx}{\textwidth}{|L|L|L|L|L|L|}
\mC{Business Understanding} &
\mC{Data Understanding} &
\mC{Data Prepartion} &
\mC{Modeling} &
\mC{Evaluation} &
\mC{Deployment}
\\ \hline
This is a long Text \newline
What about a new line
& This is another long text what a long text is that \newline
And agian an new line
& sad
& asd
& aa
& adasd
\\ \hline
\end{tabularx}
\end{table}
\end{document}