简体   繁体   中英

LaTex table - Column size and text alignment

I am quite a beginner in LaTex and generated the following table:

\documentclass{article}
\usepackage[utf8]{inputenc}


\begin{document}

\begin{table}[h]
\centering
\begin{tabular}{|p{2,5cm}||p{3,5cm}|p{2cm}| p{2cm}|}
 \hline
 \multicolumn{4}{|c|}{Initial Run of Models} \\
 \hline
  Objective & Service Level in \% &  Costs in € & Number of Lines \\
 \hline
Max. direct pass.   & 93.48    &   1,258.41  & 7\\
Min. travel time  &  77.99 & 933.13 & 7\\
 \hline
\end{tabular}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}

\end{document}

Is there any way to maintain the width of the columns and still align the text in the columns to be on the right side?

You could use the array package and add >{\raggedleft} :

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}

\begin{document}

\begin{table}[h]
\centering
\begin{tabular}{|>{\raggedleft}p{2.5cm}||>{\raggedleft}p{3.5cm}|>{\raggedleft}p{2cm}| >{\raggedleft\arraybackslash}p{2cm}|}
 \hline
 \multicolumn{4}{|c|}{Initial Run of Models} \\
 \hline
  Objective & Service Level in \% &  Costs in € & Number of Lines \\
 \hline
Max. direct pass.   & 93.48    &   1,258.41  & 7\\
Min. travel time  &  77.99 & 933.13 & 7\\
 \hline
\end{tabular}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}

\end{document}

Personally, I would suggest to use the tabularray package instead. This package makes it easy to set the alignment for row/columns or individual cells.

You could use Q[2.5cm] to specify a width, but if you don't, tabularray will determine a much better width for you.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularray}

\begin{document}

\begin{table}[h]
\centering
\begin{tblr}{
  colspec={|Q||Q|Q|Q|},
  rows={halign=r},
  column{1}={halign=l},
  row{1}={halign=c},
  row{2}={halign=l},
}
 \hline
 \SetCell[c=4]{} Initial Run of Models &&& \\
 \hline
  Objective & Service Level in \% &  Costs in € & Number of Lines \\
 \hline
Max. direct pass.   & 93.48    &   1,258.41  & 7\\
Min. travel time  &  77.99 & 933.13 & 7\\
 \hline
\end{tblr}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}

\end{document}

在此处输入图像描述

If you use the package array as suggested in the first answer, then you can define in your header one or more newcolumntype s, for example this one called R , right aligned with the width to be assigned to each column. More details here .

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}

\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

\begin{table}[h]
  \centering
  \begin{tabular}{|R{2,5cm}||R{3,5cm}|R{2cm}|R{2cm}|}
    \hline
    \multicolumn{4}{|c|}{Initial Run of Models}\\
    \hline
    Objective & Service Level in \% & Costs in € & Number of Lines\\
    \hline
    Max. direct pass. & 93.48 & 1,258.41 & 7\\
    Min. travel time  & 77.99 & 933.13 & 7\\
    \hline
  \end{tabular}
  \caption{\label{Tab:InitialRun}Initial Run of Models}
\end{table}

\end{document}

Here I changed to R all of the four columns but they don't have to be all of the same kind.

Make a search because surely there are many "duplicates" of this question here and on TeX SE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM