3

Could you please tell me how to make sure that LATEX constructs two tables in the same width? I included the Latex Code for the two tables I am trying to fit in size below. Thanks for your time, if anythin is unclear let me know, I will make an edit then.

\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}

enter image description here

3
  • Please clarify your objective: Should just the overall widths of the two tables be the same, or should each column in one table be as wide as the corresponding column in the other table?
    – Mico
    Commented Sep 18, 2020 at 10:49
  • 1
    What about an single table with two panels as in this example: i.sstatic.net/5wbmc.png
    – leandriis
    Commented Sep 18, 2020 at 10:59
  • Off-topic: The mathtools package loads the amsmath package automatically; hence no need to load amsmath explicitly. Similarly, the amssymb package loads the amsfonts package automatically; hence no need to load amsfonts separately. Finally, the amssymb package superseded the latexsym package back in ca 1994; hence, if you load amssymb, there's no reason to load latexsym.
    – Mico
    Commented Sep 18, 2020 at 13:49

3 Answers 3

2

Because the two tabulars have the same column formats, I can use this trick. I create one large tabular in a savebox, containing both tables. Then, I use \clipbox to clip out what is not needed for each individual table.

\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}

enter image description here

2

One way to assure that the overall widths of two three-column tables are the same is to (a) choose an overall width for both tables (say, 0.7\textwidth) (b) use a tabularx environment instead of a tabular environment and set the widths of both tabualarx environments to the chosen width, and (c) assign the X column type to at least one column in both tables. That way, within bounds, LaTeX can vary the widths of the X-type column(s) to make up for variations in the widths of the other columns.

In the code below, both tables' widths are set to 0.7\textwidth and the first column of both tables is assigned type X. The overall width of the third column is the same in both tables. Observe that the middle column in the second table is wider the one in the upper. The second table makes up for the increased width of the second by automatically reducing the width of the first column.

The tables are also set up in a way to assign automatic math mode to the final two columns; this allowed me to get rid of lots of $ symbols, significantly decluttering the code.

enter image description here

\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}
1

if you use \begin{table}{ p{3cm} p{8cm} } you can control the exact width of the columns. Do mind that if you want vertical rules between columns they also take a bit of width. (I do not know the exact amount)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .