如何確保兩個表的寬度相同?

如何確保兩個表的寬度相同?

你能告訴我如何確保 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的寬度列類型分配給兩個表中的至少一列。這樣,在一定範圍內,LaTeX 可以改變 -type 列的寬度,以彌補其他列寬度的變化。tabualarxXX

在下面的程式碼中,兩個表的寬度都設定為0.7\textwidth,並且兩個表的第一列都分配為 type 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} }您可以控制列的精確寬度。請注意,如果您想要列之間的垂直規則,它們也會佔用一點寬度。 (不知道具體金額)

相關內容