LaTeX forum ⇒ Graphics, Figures & TablesJoin title and horizontal table in one page Topic is solved

Information and discussion about graphics, figures & tables in LaTeX documents.
Brian_Y
Posts: 4
Joined: Mon Jun 07, 2021 10:49 pm

Join title and horizontal table in one page

Postby Brian_Y » Mon Jun 07, 2021 11:21 pm

Hi everyone!,
I spent a while reading the guide for beginners, recomendations to post and some published posts before I write my first post. I hope my questions complying with the rules.
I'm working in Stata and saving the table results using booktabs command. The real size of the table has 8 columns and 9 rows but to better show my doubt I reduced its size. The programming code for the table has no variation except for content of the result.
This is the code:
\listfiles
\documentclass{article}

\usepackage{
    booktabs,
    lscape
}

\begin{document}

\section{Title 1}
\subsection{Subtitle1}
%
\begin{landscape}
%
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{title of the table}
\begin{tabular}{l*{7}{c}}
\toprule
                    &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}&\multicolumn{1}{c}{(5)}\\
                    &\multicolumn{1}{c}{tp \_t2m\_ro}&\multicolumn{1}{c}{tp\_t2m}&\multicolumn{1}{c}{tp\_ro}&\multicolumn{1}{c}{t2m\_ro}&\multicolumn{1}{c}{tp}\\
\midrule
Dep var &                     &                     &                     &                     &                    \\
Indep var 1&      1.0003\sym{***}&      1.0004\sym{***}&      1.0003\sym{***}&                     &      1.0004\sym{***}&     \\
                    &    (0.0001)         &    (0.0001)         &    (0.0001)         &                     &    (0.0001)         &             \\
\addlinespace
Indep var 2&      1.0020\sym{**} &      1.0020\sym{**} &                     &      1.0023\sym{**} &            \\
                    &    (0.0009)         &    (0.0009)         &                     &    (0.0009)         &                         \\
\addlinespace
Indep var 3&      1.0002         &                     &      1.0002         &      1.0006\sym{***}&                     &   \\
                    &    (0.0001)         &                     &    (0.0001)         &    (0.0001)         &                     &       \\
Indep var 4&      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         &         \\
                    &         (.)         &         (.)         &         (.)         &         (.)         &         (.)         &            \\
\addlinespace
Indep var 5&      0.8563\sym{***}&      0.8549\sym{***}&      0.8585\sym{***}&      0.8595\sym{***}&      0.8572\sym{***}&     \\
                    &    (0.0327)         &    (0.0326)         &    (0.0328)         &    (0.0328)         &    (0.0327)         &       \\
\midrule
Observations        &      310623         &      310623         &      310623         &      310623         &      310623         &         \\
\bottomrule
\end{tabular}
\end{table}
%
\end{landscape}

\end{document}


I use landscape environment since the table is big and it would fit better in horizontal way.
The title is showed in one page and the table in the next one. In other posts I found that landscape will always add a page break, so I think this may be the reason.
My goal is that the both titles and table are inside one page. How can it be achieved?

Recommended reading 2021:

LaTeXguide.org • LaTeX-Cookbook.net
LaTeX Beginner's Guide LaTeX Cookbook
Bartman
Posts: 331
Joined: Fri Jan 03, 2020 2:39 pm

Join title and horizontal table in one page

Postby Bartman » Tue Jun 08, 2021 1:56 am

My suggestion uses the sideways and minipage environments and the \captionof command:

\documentclass{article}
\usepackage{booktabs}
\usepackage{rotating}% for sideways environment
% Provides the \captionof command 
% and increases the space between table and caption.
\usepackage{caption}
\usepackage{showframe}% draws the page layout diagram

\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}

\begin{document}
\section{Title 1}
\subsection{Subtitle 1}
\vfill
\begin{center}
\begin{sideways}
\begin{minipage}{\linewidth}
\captionof{table}{title of the table}
\begin{tabular}{l*{7}{c}}
\toprule
                    & (1) & (2) & (3) & (4) & (5)\\
                    & tp\_t2m\_ro & tp\_t2m & tp\_ro & t2m\_ro & tp\\
\midrule
Dep var &                     &                     &                     &                     &                    \\
Indep var 1&      1.0003\sym{***}&      1.0004\sym{***}&      1.0003\sym{***}&                     &      1.0004\sym{***}&     \\
                    &    (0.0001)         &    (0.0001)         &    (0.0001)         &                     &    (0.0001)         &             \\
\addlinespace
Indep var 2&      1.0020\sym{**} &      1.0020\sym{**} &                     &      1.0023\sym{**} &            \\
                    &    (0.0009)         &    (0.0009)         &                     &    (0.0009)         &                         \\
\addlinespace
Indep var 3&      1.0002         &                     &      1.0002         &      1.0006\sym{***}&                     &   \\
                    &    (0.0001)         &                     &    (0.0001)         &    (0.0001)         &                     &       \\
Indep var 4&      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         &         \\
                    &         (.)         &         (.)         &         (.)         &         (.)         &         (.)         &            \\
\addlinespace
Indep var 5&      0.8563\sym{***}&      0.8549\sym{***}&      0.8585\sym{***}&      0.8595\sym{***}&      0.8572\sym{***}&     \\
                    &    (0.0327)         &    (0.0326)         &    (0.0328)         &    (0.0328)         &    (0.0327)         &       \\
\midrule
Observations        &      310623         &      310623         &      310623         &      310623         &      310623         &         \\
\bottomrule
\end{tabular}
\end{minipage}
\end{sideways}
\end{center}
\vfill
\end{document}

Brian_Y
Posts: 4
Joined: Mon Jun 07, 2021 10:49 pm

Join title and horizontal table in one page

Postby Brian_Y » Tue Jun 08, 2021 11:34 pm

Bartman, thanks for you reply!
I replicated your code, and it works. Now I have two questions:
Q1 - About this command line
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}

I don't know exactly its meaning, could you explain me its function? and also why did you move it outside tabular environment?

Q2 - When I replicated with the original size of the table results (8 rows and 9 columns), it is placed further right. Looking the page layout diagram I noticed that the table not only tooks up all the space inside the diagram but also it occupies more space to the right. So, I thought maybe the solution was to modify the page layout. I found the geometry package can change the page size. I coded this:
\geometry{a4paper, total= {170mm, 265mm}}

After that the entire table fitted into the page layout diagram but it was placed at the bottom of the page instead of being in the center or middle.
What do you suggest to deal with big horizontal table? Is it convenient to modify page size or is there a better way to do it?

Bartman
Posts: 331
Joined: Fri Jan 03, 2020 2:39 pm

Join title and horizontal table in one page

Postby Bartman » Wed Jun 09, 2021 1:51 am

In my opinion, the command name \ifmmode is almost self-explanatory: if math mode. This superscripting of the argument generates an error message if it's outside the math mode.

I moved the command from the table (not tabular) environment to the preamble, because I like to separate format and content. If you need the command only in this table, it can stay there.

If there are difficulties to apply my suggestion to your real table, then better create an example where the table has the real size.

Brian_Y
Posts: 4
Joined: Mon Jun 07, 2021 10:49 pm

Join title and horizontal table in one page

Postby Brian_Y » Thu Jun 10, 2021 1:43 am

Bartman,
here I send an example of one table result with more columns and rows:

\documentclass{article}

\usepackage{
    booktabs,
    rotating,    %% for sideways environment. Provides the \captionof command and increases the space between table and caption 
    caption,
    showframe,   %% draws the page layout diagram
}

\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}

\begin{document}

\section{Title 1}
\subsection{Subtitle1}
%
\vfill
%
\begin{center}
\begin{sideways}
\begin{minipage}{\linewidth}
\captionof{table}{title of the table}

\begin{tabular}{l*{7}{c}}
\toprule
                    &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}&\multicolumn{1}{c}{(5)}&\multicolumn{1}{c}{(6)}&\multicolumn{1}{c}{(7)}\\
                    &\multicolumn{1}{c}{tp \_t2m\_ro}&\multicolumn{1}{c}{tp\_t2m}&\multicolumn{1}{c}{tp\_ro}&\multicolumn{1}{c}{t2m\_ro}&\multicolumn{1}{c}{tp}&\multicolumn{1}{c}{t2m}&\multicolumn{1}{c}{ro}\\
\midrule
Dependent variable &                     &                     &                     &                     &                     &                     &                     \\
Independent variable 1 &      1.0005\sym{**} &      1.0007\sym{***}&      1.0006\sym{**} &                     &      1.0008\sym{***}&                     &                     \\
                    &    (0.0002)         &    (0.0001)         &    (0.0003)         &                     &    (0.0002)         &                     &                     \\
\addlinespace
Independent variable 2 &      1.0101\sym{*}  &      1.0099\sym{*}  &                     &      1.0106\sym{**} &                     &      1.0104\sym{**} &                     \\
                    &    (0.0052)         &    (0.0052)         &                     &    (0.0052)         &                     &    (0.0052)         &                     \\
\addlinespace
Independent variable 3 &      1.0004         &                     &      1.0003         &      1.0011\sym{***}&                     &                     &      1.0011\sym{***}\\
                    &    (0.0005)         &                     &    (0.0005)         &    (0.0003)         &                     &                     &    (0.0003)         \\
\addlinespace
Independent variable 4 &      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         \\
\addlinespace
Independent variable 5 &      0.7991         &      0.7960         &      0.8028         &      0.8050         &      0.8003         &      0.7988         &      0.8101         \\
                    &    (0.2483)         &    (0.2467)         &    (0.2477)         &    (0.2506)         &    (0.2465)         &    (0.2468)         &    (0.2503)         \\
\addlinespace
Independent variable 6 &      1.0911         &      1.0877         &      1.0793         &      1.0984         &      1.0768         &      1.0916         &      1.0871         \\
                    &    (0.2671)         &    (0.2665)         &    (0.2683)         &    (0.2690)         &    (0.2678)         &    (0.2688)         &    (0.2705)         \\
\addlinespace
Independent variable 7 &      1.0118         &      1.0118         &      1.0358         &      1.0143         &      1.0355         &      1.0196         &      1.0400         \\
                    &    (0.1386)         &    (0.1385)         &    (0.1412)         &    (0.1392)         &    (0.1412)         &    (0.1396)         &    (0.1422)         \\
\addlinespace
Independent variable 8 &      0.8787         &      0.8770         &      0.8498         &      0.8794         &      0.8490         &      0.8696         &      0.8489         \\
                    &    (0.1375)         &    (0.1372)         &    (0.1332)         &    (0.1377)         &    (0.1331)         &    (0.1357)         &    (0.1331)         \\
\addlinespace
Independent variable 9 &      0.9700         &      0.9687         &      0.9444         &      0.9700         &      0.9439         &      0.9621         &      0.9428         \\
                    &    (0.0956)         &    (0.0955)         &    (0.0929)         &    (0.0958)         &    (0.0928)         &    (0.0946)         &    (0.0929)         \\
\addlinespace
Independent variable 10&      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         \\
\addlinespace
Independent variable 11 &      2.0395\sym{***}&      2.0481\sym{***}&      2.1164\sym{***}&      2.0325\sym{***}&      2.1219\sym{***}&      2.0757\sym{***}&      2.1131\sym{***}\\
                    &    (0.5461)         &    (0.5516)         &    (0.5846)         &    (0.5411)         &    (0.5885)         &    (0.5651)         &    (0.5805)         \\
\addlinespace
Independent variable 12 &      1.1893         &      1.1893         &      1.1900         &      1.1901         &      1.1900         &      1.1921         &      1.1910         \\
                    &    (0.1288)         &    (0.1287)         &    (0.1292)         &    (0.1293)         &    (0.1291)         &    (0.1292)         &    (0.1297)         \\
\addlinespace
Independent variable 13 &      1.2124         &      1.2138         &      1.2264         &      1.2080         &      1.2273         &      1.2068         &      1.2222         \\
                    &    (0.1556)         &    (0.1558)         &    (0.1584)         &    (0.1556)         &    (0.1584)         &    (0.1554)         &    (0.1583)         \\
\addlinespace
Independent variable 14 &      1.4634\sym{**} &      1.4617\sym{**} &      1.4409\sym{**} &      1.4630\sym{**} &      1.4399\sym{**} &      1.4506\sym{**} &      1.4393\sym{**} \\
                    &    (0.2603)         &    (0.2599)         &    (0.2570)         &    (0.2603)         &    (0.2567)         &    (0.2577)         &    (0.2569)         \\
\addlinespace
Independent variable 15 &      0.9147         &      0.9122         &      0.9294         &      0.9132         &      0.9274         &      0.8941         &      0.9286         \\
                    &    (0.0974)         &    (0.0967)         &    (0.0989)         &    (0.0977)         &    (0.0982)         &    (0.0955)         &    (0.0993)         \\
\midrule
Observations        &      310623         &      310623         &      310623         &      310623         &      310623         &      310623         &      310623         \\
\bottomrule
\end{tabular}
\end{minipage}
\end{sideways}
\end{center}

\end{document}


I'm open to suggestions. ;)

Bartman
Posts: 331
Joined: Fri Jan 03, 2020 2:39 pm

Join title and horizontal table in one page  Topic is solved

Postby Bartman » Thu Jun 10, 2021 6:44 am

You could reduce the font size and the right and left margin on the relevant page.

\documentclass[a4paper]{article}
\usepackage[
    pass,
    showframe% % draws the page layout diagram
]{geometry}
\usepackage{
    booktabs,
    rotating,    %% for sideways environment. Provides the \captionof command and increases the space between table and caption 
    caption,
    blindtext
}

\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}

\begin{document}
\section{Section}
\blindtext

\newgeometry{hmargin=3cm}% reduce horizontal margins
\section{Section}
\subsection{Subsection}
%
\vfill
%
\begin{center}
\begin{sideways}
\begin{minipage}{\linewidth}
\captionof{table}{title of the table}
\footnotesize% reduce font size
\begin{tabular}{l*{7}{c}}
\toprule
                    &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}&\multicolumn{1}{c}{(5)}&\multicolumn{1}{c}{(6)}&\multicolumn{1}{c}{(7)}\\
                    &\multicolumn{1}{c}{tp\_t2m\_ro}&\multicolumn{1}{c}{tp\_t2m}&\multicolumn{1}{c}{tp\_ro}&\multicolumn{1}{c}{t2m\_ro}&\multicolumn{1}{c}{tp}&\multicolumn{1}{c}{t2m}&\multicolumn{1}{c}{ro}\\
\midrule
Dependent variable &                     &                     &                     &                     &                     &                     &                     \\
Independent variable 1 &      1.0005\sym{**} &      1.0007\sym{***}&      1.0006\sym{**} &                     &      1.0008\sym{***}&                     &                     \\
                    &    (0.0002)         &    (0.0001)         &    (0.0003)         &                     &    (0.0002)         &                     &                     \\
\addlinespace
Independent variable 2 &      1.0101\sym{*}  &      1.0099\sym{*}  &                     &      1.0106\sym{**} &                     &      1.0104\sym{**} &                     \\
                    &    (0.0052)         &    (0.0052)         &                     &    (0.0052)         &                     &    (0.0052)         &                     \\
\addlinespace
Independent variable 3 &      1.0004         &                     &      1.0003         &      1.0011\sym{***}&                     &                     &      1.0011\sym{***}\\
                    &    (0.0005)         &                     &    (0.0005)         &    (0.0003)         &                     &                     &    (0.0003)         \\
\addlinespace
Independent variable 4 &      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         \\
\addlinespace
Independent variable 5 &      0.7991         &      0.7960         &      0.8028         &      0.8050         &      0.8003         &      0.7988         &      0.8101         \\
                    &    (0.2483)         &    (0.2467)         &    (0.2477)         &    (0.2506)         &    (0.2465)         &    (0.2468)         &    (0.2503)         \\
\addlinespace
Independent variable 6 &      1.0911         &      1.0877         &      1.0793         &      1.0984         &      1.0768         &      1.0916         &      1.0871         \\
                    &    (0.2671)         &    (0.2665)         &    (0.2683)         &    (0.2690)         &    (0.2678)         &    (0.2688)         &    (0.2705)         \\
\addlinespace
Independent variable 7 &      1.0118         &      1.0118         &      1.0358         &      1.0143         &      1.0355         &      1.0196         &      1.0400         \\
                    &    (0.1386)         &    (0.1385)         &    (0.1412)         &    (0.1392)         &    (0.1412)         &    (0.1396)         &    (0.1422)         \\
\addlinespace
Independent variable 8 &      0.8787         &      0.8770         &      0.8498         &      0.8794         &      0.8490         &      0.8696         &      0.8489         \\
                    &    (0.1375)         &    (0.1372)         &    (0.1332)         &    (0.1377)         &    (0.1331)         &    (0.1357)         &    (0.1331)         \\
\addlinespace
Independent variable 9 &      0.9700         &      0.9687         &      0.9444         &      0.9700         &      0.9439         &      0.9621         &      0.9428         \\
                    &    (0.0956)         &    (0.0955)         &    (0.0929)         &    (0.0958)         &    (0.0928)         &    (0.0946)         &    (0.0929)         \\
\addlinespace
Independent variable 10&      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         &      1.0000         \\
\addlinespace
Independent variable 11 &      2.0395\sym{***}&      2.0481\sym{***}&      2.1164\sym{***}&      2.0325\sym{***}&      2.1219\sym{***}&      2.0757\sym{***}&      2.1131\sym{***}\\
                    &    (0.5461)         &    (0.5516)         &    (0.5846)         &    (0.5411)         &    (0.5885)         &    (0.5651)         &    (0.5805)         \\
\addlinespace
Independent variable 12 &      1.1893         &      1.1893         &      1.1900         &      1.1901         &      1.1900         &      1.1921         &      1.1910         \\
                    &    (0.1288)         &    (0.1287)         &    (0.1292)         &    (0.1293)         &    (0.1291)         &    (0.1292)         &    (0.1297)         \\
\addlinespace
Independent variable 13 &      1.2124         &      1.2138         &      1.2264         &      1.2080         &      1.2273         &      1.2068         &      1.2222         \\
                    &    (0.1556)         &    (0.1558)         &    (0.1584)         &    (0.1556)         &    (0.1584)         &    (0.1554)         &    (0.1583)         \\
\addlinespace
Independent variable 14 &      1.4634\sym{**} &      1.4617\sym{**} &      1.4409\sym{**} &      1.4630\sym{**} &      1.4399\sym{**} &      1.4506\sym{**} &      1.4393\sym{**} \\
                    &    (0.2603)         &    (0.2599)         &    (0.2570)         &    (0.2603)         &    (0.2567)         &    (0.2577)         &    (0.2569)         \\
\addlinespace
Independent variable 15 &      0.9147         &      0.9122         &      0.9294         &      0.9132         &      0.9274         &      0.8941         &      0.9286         \\
                    &    (0.0974)         &    (0.0967)         &    (0.0989)         &    (0.0977)         &    (0.0982)         &    (0.0955)         &    (0.0993)         \\
\midrule
Observations        &      310623         &      310623         &      310623         &      310623         &      310623         &      310623         &      310623         \\
\bottomrule
\end{tabular}
\end{minipage}
\end{sideways}
\end{center}
\restoregeometry% restore the previous margin settings

\section{Section}
\blindtext
\end{document}

Brian_Y
Posts: 4
Joined: Mon Jun 07, 2021 10:49 pm

Join title and horizontal table in one page

Postby Brian_Y » Tue Jun 15, 2021 5:42 am

Bartman, thanks for all your replies, they helped me!


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 6 guests