3

I have a problem. I want to create a table in Latex. The heading column (the first row) should be centred. After that, the next text should be left-aligned and start at the top, so it should not be centre-aligned, but at the top. Furthermore, I would like that, for example, if I have some longer text, that this should automatically have line breaks for this I have tabular x. However, the whole thing doesn't work for me and the line is missing from the first row.

enter image description here

enter image description here

\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}
7
  • 1
    The command \renewcommand\tabularxcolumn[1]{m{#1}} centers your text vertically. If you don't want that (i.e. want your text to be top-aligned), you should remove this command. And you don't have a top horizontal line because you didn't insert one. For that, you need to add a \hline command before the first row.
    – Imran
    Commented Jun 5, 2022 at 8:44
  • Thank you! I removed the command but the text is not still left-aligned top and \hline worked.
    – Test
    Commented Jun 5, 2022 at 8:54
  • I cannot compile your test document, as I get an error message about \fancyheadoffset not being defined. Please fix.
    – Mico
    Commented Jun 5, 2022 at 9:03
  • @Test That should have worked. I just checked that for confirmation. Here(imgur.com/zedSH5A) is the output. If possible you may reproduce the error with an MWE.
    – Imran
    Commented Jun 5, 2022 at 9:10
  • @Mico sorry for the inconvenience. I edited my MVP.
    – Test
    Commented Jun 5, 2022 at 9:13

2 Answers 2

3

As I commented, removing the command The command \renewcommand\tabularxcolumn[1]{m{#1}} aligns the text at the top. And to add a rule, you need a \hline command.

enter image description here

\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}

However, I suggest a table with few horizontal rules and no vertical rules.

enter image description here

\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}

Here, I have used the tabularray package which makes managing table very convenient.

1
  • 1
    The first column are much wider than the other four. This wouldn't be necessary if you replaced BusinessUnderstanding with Business Understanding. I would also replace full justification in the narrow columns with ragged-right layout.
    – Mico
    Commented Jun 5, 2022 at 9:47
2

You're employing a tabularx environment, but all six X column definitions are overridden by the \multicolumn{1}{c}{...} directives, which employ the c column type, which doesn't allow automatic line breaking. In short, you're not letting tabularx do its job. In the solution below, I suggest using the C column type instead, where C is defined as a centered/bold-faced version of X.

A separate issue: Get rid of the \renewcommand\tabularxcolumn[1]{m{#1}} instruction since -- a fact that you note in the comments ... -- it serves to vertically center the column contents, which is something you don't want.

enter image description here

\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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .