我正在尝试在 A4 纸上制作一个矩形网格(5 行、4 列)。所有单元格的尺寸均为 5cm x 5cm。为此,我使用了一个表格,如代码所示。此表格可以通过向左和向上移动一点来适应纸张。单元格的大小是固定的,并且必须保持与内容无关,内容可以是图片或使用 \makecell 命令创建的几行文本。我尝试过,但它不适用于单元格的高度,它一直在变化。我做错了什么?
祝好, Dimitri
\documentclass[12pt, a4paper]{article}
\usepackage{amsmath}
\usepackage{pdfpages}
\usepackage{array}
\usepackage{makecell}
\graphicspath{{./}{./Pictures/}}
\renewcommand{\arraystretch}{0}
\renewcommand{\tabcolsep}{0cm}
\begin{document}
\thispagestyle{empty}
\begin{table}
\begin{center}
\vspace*{-2.5cm}
\hspace*{-3.25cm}
\begin{tabular}{|m{5cm}|m{5cm}|m{5cm}|m{5cm}|}
\hline
\centering{\includegraphics[width=4.5cm,height=4.5cm,keepaspectratio]{Stanford_Bunny}} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}\\[5cm]
\hline
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}\\[5cm]
\hline
\makecell{\textbf{line 1}} &
\makecell{\textbf{line 1}} &
\makecell{\textbf{line 1}} &
\makecell{\textbf{line 1}}\\[5cm]
\hline
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}\\[5cm]
\hline
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6} &
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}\\[5cm]
\hline
\end{tabular}%
\end{center}
\end{table}
\end{document}
输出如下(最后一列也表现不好):
答案1
两种可能性
下面我描述两种不同的解决方案
我在答案的末尾附上了代码calstable
,但由于两个解决方案的输出相同,因此我没有附上结果的图片。
表格及其朋友
第一种可能性是在表格中添加一个“幻像”列,其垂直线不可见,为 5 厘米。我已通过此方法定义了一种新的列类型。由于您有四列宽度相等,因此我也将它们定义为一种新的列类型。然后,我可以添加必要的命令,使内容水平居中在一个位置。
请注意表格的宽度不是20 厘米。您必须添加五条垂直规则的宽度(arrayrulewidth
标准为 0.4pt,IE2 pt)。因此,我不得不更改 MWE 的左右边距以删除一条overfull hbox
消息。高度也类似,为 25 cm + 6 arrayrulewidth
。
编辑20190807:为了确保表格根据纸张大小(而不是边距)在页面上垂直和水平居中,我将表格放在一个基于的新环境中Andrew Swann 的回答在这里。
为了使行与行之间有足够的0.9cm
空间(正如您在评论中提到的那样),我“作弊”了,\hline\hline
在行之间添加了双垂直线( ),并将两个线之间的空间增加到0.9cm minus two \arrayrulewidth
s。为了避免第一页空白,我将所有文档边距减少到0cm
。
\usepackage[left=0cm,top=0cm,right=0cm,bottom=0cm]{geometry} % Zero margin
\setlength\doublerulesep{\dimexpr(0.9cm-(2\arrayrulewidth))} % Increased space between horizontal rules
您已将表格置于浮动环境中,但这种类型的全页表格不应浮动。
\documentclass[12pt, a4paper, demo]{article}
\usepackage{amsmath}
\usepackage{pdfpages}
\usepackage{array, hhline}
\usepackage[left=0cm,top=0cm,right=0cm,bottom=0cm]{geometry}
%% This code is modified from Andrew Swann's code in this answer
%% https://tex.stackexchange.com/a/154736/9632
\newenvironment{preface}{\clearpage\setlength{\topskip}{0pt}%
\newgeometry{margin=0cm,ignoreheadfoot}%
\thispagestyle{empty}%
\centering%
\vspace*{\fill}\nointerlineskip\small}%
{\vspace*{\fill}\clearpage\restoregeometry}
%\graphicspath{{./}{./Pictures/}}
\renewcommand{\arraystretch}{0}
\renewcommand{\tabcolsep}{0cm}
\newcolumntype{M}{>{\centering\arraybackslash}m{5cm}}
\newcolumntype{F}{>{\rule{0pt}{5cm}}m{0pt}}
\begin{document}
\pagestyle{empty}
\begin{preface}
\setlength\doublerulesep{\dimexpr(0.9cm-(2\arrayrulewidth))}
\begin{tabular}{*{4}{|M}|@{}F@{}}
\hline
\includegraphics[width=4.5cm,height=4.5cm,keepaspectratio]{bunny}
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&\\
\hline\hline
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&\\
\hline\hline
\textbf{line 1}
&
\textbf{line 1}
&
\textbf{line 1}
&
\textbf{line 1}
&\\
\hline\hline
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&\\
\hline\hline
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&
\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6
&\\
\hline
\end{tabular}\par
\end{preface}
\end{document}
卡尔斯特布尔
我认为,cals 包更适合这种类型的表格,因为它可以很容易地固定列和行的宽度和高度。但是,在 cals 中使用“幻影”线也是可行的。
行与行之间有 0.9 厘米的间隙,可以通过增加该高度的行轻松修复。
在 cals 中,该命令\cals@setpadding{<wd>}
相当于tabcolsep
,但它还设置了行距。由于规则(边框)是幻影,我将填充设置为0.3pt
,这应该会导致内部单元格为 5cmx5cm。
该命令\alignC
使单元格内容水平居中,并一直有效直到被另一个对齐命令关闭。\vfil
使单元格内容垂直居中,并且可以通过定义命令进行全局设置cals@AtBeginCell
,即\def\cals@AtBeginCell{\vfil}
(您也可以使用\newcommand*
或\renewcommand*
)。
还请查看代码中的注释。
\documentclass[12pt, a4paper, demo]{article}
\usepackage{amsmath}
\usepackage{pdfpages}
\usepackage{cals}
\usepackage[left=0cm,top=0cm,right=0cm,bottom=0cm]{geometry}
%% This code is modified from Andrew Swann's code in this answer
%% https://tex.stackexchange.com/a/154736/9632
\newenvironment{preface}{\clearpage\setlength{\topskip}{0pt}%
\newgeometry{margin=0cm,ignoreheadfoot}%
\thispagestyle{empty}%
\vspace*{\fill}\nointerlineskip\small}%
{\vspace*{\fill}\clearpage\restoregeometry}
%\graphicspath{{./}{./Pictures/}}
\begin{document}
\pagestyle{empty}
\begin{preface}
\begin{calstable}[c] % Centre the calstable
% Defining 4 equal columns
\colwidths{{5cm}{5cm}{5cm}{5cm}
}
% Set up the tabular
\makeatletter
\def\cals@framers@width{0.3pt} % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.03pt}
\def\cals@bodyrs@width{0.3pt}
\cals@setpadding{0.3pt} % To ensure that the inside of cell is 5 cm
\def\cals@cs@width{0.3pt} % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.3pt}
\def\rb{\ifx\cals@borderR\relax % Right Border switch (off-on)
\def\cals@borderR{0.0pt}
\else \let\cals@borderR\relax\fi}
\def\lb{\ifx\cals@borderL\relax % Left Border switch (off-on)
\def\cals@borderL{0.0pt}
\else \let\cals@borderL\relax\fi}
\def\cals@AtBeginCell{\vfil} % Vertically centre content of all cell
%\def\cals@AtEndCell{}
%% Start of tabular body, no header row
% R1B1
\brow
\alignC\cell{\includegraphics[width=4.5cm,height=4.5cm,keepaspectratio]{bunny}}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\ht\cals@current@row=5cm
\erow
% R2B1b %% Fore creating space between rows
\brow
\lb\rb\cell{} % \lb&\rb - shortcuts for tuning off left and right border this row
\cell{}
\cell{}
\cell{}\lb\rb % use shortcuts for tuning on left and right border for next row
\ht\cals@current@row=0.9cm
\erow
%R3B2
\brow
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\ht\cals@current@row=5cm
\erow
% R4B2b
\brow
\lb\rb\cell{}
\cell{}
\cell{}
\cell{}\lb\rb
\ht\cals@current@row=0.9cm
\erow
%R5B3
\brow
\cell{\textbf{line 1}}
\cell{\textbf{line 1}}
\cell{\textbf{line 1}}
\cell{\textbf{line 1}}
\ht\cals@current@row=5cm
\erow
% R6B3b
\brow
\lb\rb\cell{}
\cell{}
\cell{}
\cell{}\lb\rb
\ht\cals@current@row=0.9cm
\erow
%R7B4
\brow
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\ht\cals@current@row=5cm
\erow
% R8B4b
\brow
\lb\rb\cell{}
\cell{}
\cell{}
\cell{}\lb\rb
\ht\cals@current@row=0.9cm
\erow
%R2B5
\brow
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\cell{\textbf{line 1} \par line 2 \par line 3 \par line 4 \par line 5 \par line 6}
\ht\cals@current@row=5cm
\erow
\end{calstable}\par
\end{preface}
\end{document}
答案2
经过 11 个小时的搜索,我找到了一个解决方案,我想与大家分享一下。代码相当容易理解,其本质是表格内的迷你页面环境。请注意,仅使用此迷你页面设置中的第一列即可解决问题,但我将其保留在所有单元格中,以便于识别我稍后所做的操作。还请注意,可以使用的“页面空间”的边距可以根据需要进行调整以适合网格。大概有更短的方法来重复迷你页面输入,但我不知道这是怎么做的。希望这对某些人有帮助。
祝好, Dimitri
\documentclass[12pt, a4paper]{article}
\usepackage{amsmath}
\usepackage{pdfpages}
\usepackage{array}
\usepackage{makecell}
\usepackage[export]{adjustbox}
\usepackage[left=0.5cm,top=2.35cm,right=0.5cm,bottom=2.35cm]{geometry}
\graphicspath{{./}{./Pictures/}}
\renewcommand{\arraystretch}{0}
\renewcommand{\tabcolsep}{0cm}
\begin{document}
\pagestyle{empty}
\begin{table}[h!]
\begin{tabular}{|m{5cm}|m{5cm}|m{5cm}|m{5cm}|}
\hline
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\includegraphics[width=4.5cm,height=4.5cm,keepaspectratio]{Bunny}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage}\\
\hline
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage}\\
\hline
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1}}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1}}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1}}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1}}
\end{center}
\end{minipage}\\
\hline
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage}\\
\hline
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage} &
\begin{minipage}[c][5cm][c]{5cm}
\begin{center}
\makecell{\textbf{line 1} \\ line 2 \\ line 3 \\ line 4 \\ line 5 \\ line 6}
\end{center}
\end{minipage}\\
\hline
\end{tabular}
\end{table}
\end{document}
输出现在如下所示: