TransWikia.com

subtable does not align properly

TeX - LaTeX Asked by Paolo on January 26, 2021

I tried to align 2 tables to save space using subtables as shown here. So the same piece of code it turns out to be like this:
enter image description here

To ease the reader I will attach the piece of latex here:

documentclass{article} % For LaTeX2e
usepackage{iclr2021_conference,times}

% Optional math commands from https://github.com/goodfeli/dlbook_notation.
input{math_commands.tex}

usepackage{hyperref}
usepackage{url}
% add by yang
usepackage{graphicx}
usepackage{graphics}
usepackage{color}
usepackage{mathtools}
usepackage{multirow}
usepackage{subfigure}
usepackage{tabularx}
usepackage{amsfonts}
usepackage{wrapfig,lipsum,booktabs}
usepackage{xcolor}
usepackage{hyperref}
usepackage{floatflt}
usepackage{wrapfig,lipsum,booktabs}
usepackage{amssymb}
usepackage{caption}
usepackage{subcaption}
begin{table}[h]
centering
    begin{subtable}[c]{0.45textwidth}
        centering
        begin{tabular}{l | l | l}
        Day & Max Temp & Min Temp 
        hline hline
        Mon & 20 & 13
        Tue & 22 & 14
        Wed & 23 & 12
        Thurs & 25 & 13
        Fri & 18 & 7
        Sat & 15 & 13
        Sun & 20 & 13
       end{tabular}
       caption{First Week}
       label{tab:week1}
    end{subtable}
    hfill
    begin{subtable}[c]{0.45textwidth}
        centering
        begin{tabular}{l | l | l}
        Day & Max Temp & Min Temp 
        hline hline
        Mon & 17 & 11
        Tue & 16 & 10
        Wed & 14 & 8
        Thurs & 12 & 5
        Fri & 15 & 7
        Sat & 16 & 12
        Sun & 15 & 9
        end{tabular}
        caption{Second Week}
        label{tab:week2}
     end{subtable}
     caption{Max and min temps recorded in the first two weeks of July}
     label{tab:temps}
end{table}

Any thoughts why I get these tabs misaligned?

— edit —
I have added the packages.

One Answer

A minimal (non)working example that reproduces something close to your screenshot would be the following:

enter image description here

documentclass{article}

begin{document}

begin{table}[h]
centering
    begin{subtable}[c]{0.45textwidth}
        centering
        begin{tabular}{l | l | l}
        Day & Max Temp & Min Temp 
        hline hline
        Mon & 20 & 13
       end{tabular}
       caption{First Week}
       label{tab:week1}
    end{subtable}
    hfill
    begin{subtable}[c]{0.45textwidth}
        centering
        begin{tabular}{l | l | l}
        Day & Max Temp & Min Temp 
        hline hline
        Mon & 17 & 11
        end{tabular}
        caption{Second Week}
        label{tab:week2}
     end{subtable}
     caption{Max and min temps recorded in the first two weeks of July}
     label{tab:temps}
end{table}

end{document}

Trying to compile the above example results in a bunch of error message with the first one being

 LaTeX Error: Environment subtable undefined.

This error message is shown because you fogrot to add the subcaption package, which defines the subtable environment. Fixin the above example by adding usepackage{subcaption} to the preamble results in the following code that compiles without error messages and results in the following expected output:

enter image description here

documentclass{article}
usepackage{subcaption}
begin{document}

begin{table}[h]
centering
    begin{subtable}[c]{0.45textwidth}
        centering
        begin{tabular}{l | l | l}
        Day & Max Temp & Min Temp 
        hline hline
        Mon & 20 & 13
       end{tabular}
       caption{First Week}
       label{tab:week1}
    end{subtable}
    hfill
    begin{subtable}[c]{0.45textwidth}
        centering
        begin{tabular}{l | l | l}
        Day & Max Temp & Min Temp 
        hline hline
        Mon & 17 & 11
        end{tabular}
        caption{Second Week}
        label{tab:week2}
     end{subtable}
     caption{Max and min temps recorded in the first two weeks of July}
     label{tab:temps}
end{table}


end{document}

Conclusion: Do not ignore error messages! Even if you get something that on first glance resembles a pdf file, there can still be issues with it. After an error, TeX only tries to recover enough to syntax check more of the file, it does not try to make sensible output after an error. Thus, try to fix the all error messages before even taking a look at the resulting pdf file. If you don't know what to do in order to fix such an error message, you could ask a question explicitly mentioning the error messages you get.

Since you mentioned one of the Overleaf How To... pages, I assume you use Overleaf to compile your code. If that's correct, you should see someting like the following:

enter image description here

If you click on the button with the "6" inside of a red rectangle you can see the error messages. Only take a look at the output, if there is no red rectangle. (= no error messages)

Answered by leandriis on January 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP