表格宽度与标题宽度相同

表格宽度与标题宽度相同

我有一张有 5 列的表格。我希望表格的宽度与标题的宽度一样宽。如下所示,我为每一列分配了宽度,并且水平线超出了标题线的边界。我可以:

  • 指定前 4 列的宽度
  • 并保留最后一列的宽度以适合标题行边界?

以下是一小段代码:

\documentclass[a4paper]{article}
\usepackage{luamplib}
\usepackage{fancyhdr}
\usepackage{calc}
\usepackage[lmargin=0.80cm,rmargin=0.80cm,tmargin=2.50cm,bmargin=0.80cm]{geometry}
\usepackage{upgreek}
\usepackage{amsbsy}
\usepackage{amssymb}
\usepackage[sfdefault]{carlito}
\usepackage{array}
\newcolumntype{?}{!{\vrule width 1pt}}
\parindent=0pt
\linespread{1.00}   %the factor multiplied to the current `baselineskip'

\usepackage{graphicx}
\graphicspath{{../../texmf/img/}}

\usepackage{xcolor}

\pagestyle{fancy}
\fancyhf{}
\headsep=2.0mm

\usepackage{datetime}   %Change format of `\today' with commands for current time
\renewcommand{\dateseparator}{-}

\newcommand{\headertoday}{\the\year \dateseparator \twodigit\month \dateseparator \twodigit\day}

%### define an environment to have a local `\arraystretch'
\newenvironment{TabLocalVerticalSpacing}[1]
  {\def\arraystretch{#1}\tabular}
  {\endtabular}
%#####################################

\newcommand\leftheadercalc[1]{%
\begin{TabLocalVerticalSpacing}{#1}[b]{@{}ll}\footnotesize{balabala}&\footnotesize{}\\\footnotesize{Project: ??????}&\footnotesize{}\end{TabLocalVerticalSpacing}%
}
\newcommand\rightheadercalc[1]{%
\begin{TabLocalVerticalSpacing}{#1}[b]{rr@{}}\footnotesize{Date:}&\footnotesize{\headertoday}\\\footnotesize{Page:}&\footnotesize{\thepage}\end{TabLocalVerticalSpacing}%
}
\newcommand\midheadercalc[1]{%
\begin{TabLocalVerticalSpacing}{#1}[b]{cc}\footnotesize{}&\footnotesize{}\\\footnotesize{}&\footnotesize{}\end{TabLocalVerticalSpacing}%
}
\settototalheight\headheight{\maxof{\leftheadercalc{0.600000}}{\rightheadercalc{0.600000}}{\midheadercalc{0.600000}}}

\lhead{\leftheadercalc{0.600000}}
\rhead{\rightheadercalc{0.600000}}
\chead{\midheadercalc{0.600000}}
\newcommand\Tstrut{\rule{0pt}{3ex}}         % `top' strut
\newcommand\Bstrut{\rule[-0.9ex]{0pt}{0pt}} % `bottom' strut

\newcommand{\defaultfontsize}{\fontsize{10pt}{\baselineskip}\selectfont}
\begin{document}\defaultfontsize

\begin{tabular}{@{} p{1.5cm} p{12cm} p{1.5cm} p{1cm} p{2cm}}
Design to & \multicolumn{2}{l}{Steel structures AS 4100鈥?998} &&\\[1ex]
\hline
1 & Given data &Foo&Bar&cl5.2.3\Tstrut\\

\end{tabular}

\end{document}

PDF 版本如下所示: 在此处输入图片描述

谢谢。

答案1

这看起来像是 tabularx 的工作。

\documentclass[a4paper]{article}
%\usepackage{luamplib}% easier to remove then get to work
\usepackage{fancyhdr}
\usepackage{calc}
\usepackage[lmargin=0.80cm,rmargin=0.80cm,tmargin=2.50cm,bmargin=0.80cm]{geometry}
\usepackage{upgreek}
\usepackage{amsbsy}
\usepackage{amssymb}
%\usepackage[sfdefault]{carlito}
\usepackage{array}
\newcolumntype{?}{!{\vrule width 1pt}}
\parindent=0pt
\linespread{1.00}   %the factor multiplied to the current `baselineskip'

\usepackage{graphicx}
\graphicspath{{../../texmf/img/}}

\usepackage{xcolor}

\pagestyle{fancy}
\fancyhf{}
\headsep=2.0mm

\usepackage{datetime}   %Change format of `\today' with commands for current time
\renewcommand{\dateseparator}{-}

\newcommand{\headertoday}{\the\year \dateseparator \twodigit\month \dateseparator \twodigit\day}

%### define an environment to have a local `\arraystretch'
\newenvironment{TabLocalVerticalSpacing}[1]
  {\def\arraystretch{#1}\tabular}
  {\endtabular}

\usepackage{tabularx}
%#####################################

\newcommand\leftheadercalc[1]{%
\begin{TabLocalVerticalSpacing}{#1}[b]{@{}ll}\footnotesize{balabala}&\footnotesize{}\\\footnotesize{Project: ??????}&\footnotesize{}\end{TabLocalVerticalSpacing}%
}
\newcommand\rightheadercalc[1]{%
\begin{TabLocalVerticalSpacing}{#1}[b]{rr@{}}\footnotesize{Date:}&\footnotesize{\headertoday}\\\footnotesize{Page:}&\footnotesize{\thepage}\end{TabLocalVerticalSpacing}%
}
\newcommand\midheadercalc[1]{%
\begin{TabLocalVerticalSpacing}{#1}[b]{cc}\footnotesize{}&\footnotesize{}\\\footnotesize{}&\footnotesize{}\end{TabLocalVerticalSpacing}%
}
\settototalheight\headheight{\maxof{\leftheadercalc{0.600000}}{\rightheadercalc{0.600000}}{\midheadercalc{0.600000}}}

\lhead{\leftheadercalc{0.600000}}
\rhead{\rightheadercalc{0.600000}}
\chead{\midheadercalc{0.600000}}
\newcommand\Tstrut{\rule{0pt}{3ex}}         % `top' strut
\newcommand\Bstrut{\rule[-0.9ex]{0pt}{0pt}} % `bottom' strut

\newcommand{\defaultfontsize}{\fontsize{10pt}{\baselineskip}\selectfont}
\begin{document}\defaultfontsize

\noindent\begin{tabularx}{\textwidth}{@{} p{1.5cm} X p{1.5cm} p{1cm} p{2cm}}
Design to & \multicolumn{2}{l}{Steel structures AS 4100鈥?998} &&\\[1ex]
\hline
1 & Given data &Foo&Bar&cl5.2.3\Tstrut\\

\end{tabularx}

\end{document}

相关内容