您能告诉我如何确保 LATEX 构建两个宽度相同的表格吗?我在下面提供了两个表格的 Latex 代码,我试图将它们调整到合适的大小。感谢您的时间,如果有任何不清楚的地方请告诉我,我会进行编辑。
\documentclass[a4paper, 11pt, oneside]{book}
\bibliographystyle{plainnat}
\makeatletter
\makeatother
\usepackage[a4paper,left=3cm,right=3cm,top=3cm,bottom=3cm]{geometry}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{booktabs}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{latexsym}
\usepackage{lmodern}
\usepackage{mathtools}
\usepackage{mdframed}
\usepackage{pgf}
\usepackage{tcolorbox}
\usepackage[flushleft]{threeparttable}
\usepackage{tikz}
\usepackage{titlesec}
\usepackage[absolute,overlay]{textpos}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{llc}
\toprule
Operation & &Bit Complexity \\
\midrule
Addition &$a+b$ &$\mathcal{O}(\log(ab)+)$ \\
Subtraction &$a-b$ &$\mathcal{O}(\log(ab))$ \\
Multiplication &$a \cdot b$ &$\mathcal{O}(\log^2(ab))$ \\
Division with remainder &$a = k \cdot b + r$ &$\mathcal{O}(\log^2(ab))$\\
\bottomrule
\end{tabular}
\caption{Bit complexity of elementary operations in $\mathbb{Z}$.}
\label{tab:table_1}
\end{table}
\begin{table}[ht]
\centering
\begin{tabular}{llc}
\toprule
\multicolumn{2}{c}{Operation} &Bit Complexity \\
\midrule
Modular Addition &$a+b \bmod n$ &$\mathcal{O}(\log(n))$ \\
Modular Subtraction &$a-b \bmod n$ &$\mathcal{O}(\log(n))$ \\
Modular Multiplication &$a \cdot b \bmod n$ &$\mathcal{O}(\log^2(n))$ \\
Modular Inversion &$a^{-1} \bmod n$ &$\mathcal{O}(\log^2(n))$ \\
Modular Exponentiation &$a^k \bmod n$, $k < n$ &$\mathcal{O}(\log^3(n))$ \\
\bottomrule
\end{tabular}
\caption{Bit complexity of elementary operations in $\mathbb{Z} \/ n \mathbb{Z}$.}
\label{tab:table_2}
\end{table}
\end{document}
答案1
由于两个表格具有相同的列格式,因此我可以使用这个技巧。我在保存框中创建一个包含两个表格的大表格。然后,我使用\clipbox
来剪掉每个表格中不需要的内容。
\documentclass[a4paper, 11pt, oneside]{book}
\bibliographystyle{plainnat}
\makeatletter
\makeatother
\usepackage[a4paper,left=3cm,right=3cm,top=3cm,bottom=3cm]{geometry}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{booktabs}
\usepackage{etoolbox}
\usepackage{fancyhdr}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage{latexsym}
\usepackage{lmodern}
\usepackage{mathtools}
\usepackage{mdframed}
\usepackage{pgf}
\usepackage{tcolorbox}
\usepackage[flushleft]{threeparttable}
\usepackage{tikz}
\usepackage{titlesec}
\usepackage[absolute,overlay]{textpos}
\usepackage{trimclip}
\begin{document}
\newsavebox\sharedtable
\savebox\sharedtable{%
\begin{tabular}{llc}
\toprule
Operation & &Bit Complexity \\
\midrule
Addition &$a+b$ &$\mathcal{O}(\log(ab)+)$ \\
Subtraction &$a-b$ &$\mathcal{O}(\log(ab))$ \\
Multiplication &$a \cdot b$ &$\mathcal{O}(\log^2(ab))$ \\
Division with remainder &$a = k \cdot b + r$ &$\mathcal{O}(\log^2(ab))$\\
\bottomrule\\
\toprule
\multicolumn{2}{c}{Operation} &Bit Complexity \\
\midrule
Modular Addition &$a+b \bmod n$ &$\mathcal{O}(\log(n))$ \\
Modular Subtraction &$a-b \bmod n$ &$\mathcal{O}(\log(n))$ \\
Modular Multiplication &$a \cdot b \bmod n$ &$\mathcal{O}(\log^2(n))$ \\
Modular Inversion &$a^{-1} \bmod n$ &$\mathcal{O}(\log^2(n))$ \\
Modular Exponentiation &$a^k \bmod n$, $k < n$ &$\mathcal{O}(\log^3(n))$ \\
\bottomrule
\end{tabular}%
}
\begin{table}[ht]
\centering
\clipbox{0pt 107pt 0pt 0pt}{\usebox\sharedtable}
\vspace{-5pt}
\caption{Bit complexity of elementary operations in $\mathbb{Z}$.}
\label{tab:table_1}
\end{table}
\begin{table}[ht]
\centering
\clipbox{0pt 0pt 0pt 91pt}{\usebox\sharedtable}
\caption{Bit complexity of elementary operations in $\mathbb{Z} \/ n \mathbb{Z}$.}
\label{tab:table_2}
\end{table}
\end{document}
答案2
确保两个三列表格的总宽度相同的一种方法是 (a) 为两个表选择一个总宽度(例如0.7\textwidth
)(b)使用tabularx
环境而不是环境并将两个环境tabular
的宽度设置为所选宽度,以及 (c) 将列类型分配给两个表中的至少一个列。这样,在一定范围内,LaTeX 可以改变 -type 列的宽度以弥补其他列宽度的变化。tabualarx
X
X
在下面的代码中,两个表的宽度都设置为0.7\textwidth
,并且两个表的第一列都分配了类型X
。两个表中第三列的整体宽度相同。观察第二个表中的中间列比上面的列更宽。第二个表通过自动减小第一列的宽度来弥补第二个表增加的宽度。
表格还设置为将自动数学模式分配给最后两列;这使我能够摆脱大量$
符号,从而显著整理代码。
\documentclass[a4paper, 11pt, oneside]{book}
\bibliographystyle{plainnat}
\usepackage[margin=3cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{mathtools,amssymb,amsthm}
\usepackage{etoolbox,fancyhdr,graphicx}
\usepackage{tabularx,booktabs,lmodern}
\newcolumntype{C}{>{$}c<{$}} % automatic math mode, centered
\newcolumntype{L}{>{$}l<{$}} % automatic math mode, left-aligned
\usepackage{lmodern}
\usepackage{mdframed,pgf,tikz,tcolorbox}
\usepackage[flushleft]{threeparttable}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabularx}{0.7\textwidth}{@{}XLC@{}}
\toprule
Operation & & $Bit Complexity$ \\
\midrule
Addition &a+b &\mathcal{O}(\log(ab)+) \\
Subtraction &a-b &\mathcal{O}(\log(ab)) \\
Multiplication &a \cdot b &\mathcal{O}(\log^2(ab)) \\
Division with remainder &a = k \cdot b + r &\mathcal{O}(\log^2(ab))\\
\bottomrule
\end{tabularx}
\caption{Bit complexity of elementary operations in $\mathbb{Z}$.}
\label{tab:table_1}
\vspace{8mm}
\begin{tabularx}{0.7\textwidth}{@{}XLC@{}}
\toprule
\multicolumn{2}{@{}c}{Operation} & $Bit Complexity$ \\
\midrule
Modular Addition &a+b \bmod n &\mathcal{O}(\log(n)) \\
Modular Subtraction &a-b \bmod n &\mathcal{O}(\log(n)) \\
Modular Multiplication &a \cdot b \bmod n &\mathcal{O}(\log^2(n)) \\
Modular Inversion &a^{-1} \bmod n &\mathcal{O}(\log^2(n)) \\
Modular Exponentiation &a^k \bmod n,\ k < n &\mathcal{O}(\log^3(n)) \\
\bottomrule
\end{tabularx}
\caption{Bit complexity of elementary operations in $\mathbb{Z} \/ n \mathbb{Z}$.}
\label{tab:table_2}
\end{table}
\end{document}
答案3
如果您使用,\begin{table}{ p{3cm} p{8cm} }
您可以控制列的确切宽度。请注意,如果您希望列之间有垂直规则,它们也会占用一点宽度。(我不知道确切的数量)