Graphics, Figures & TablesCustomise environment for table formatting

Information and discussion about graphics, figures & tables in LaTeX documents.
Alexander1994
Posts: 7
Joined: Sat Apr 10, 2021 10:29 am

Customise environment for table formatting

Post by Alexander1994 »

Hello everybody! I hope you are all doing well.

I am new to Latex. I have been recommended Overleaf, which I like as there are templates for theses - which is my current project.

However, as I produce many tables in my thesis, I would like to add further formatting which I would like to always be present in any table I produce. Table formatting is done in the table with "renewenvironment". See the code - which works fine - just below.

Code: Select all

% Table formatting
\let\table@default\table
\let\endtable@default\endtable

\define@key{table}{caption}{\newcommand{\table@caption}{#1}}
\define@key{table}{toc}{\newcommand{\table@toc}{#1}}
\define@key{table}{label}{\newcommand{\table@label}{#1}}
\define@key{table}{columns}{\newcommand{\table@columns}{#1}}
\define@key{table}{placement}{\newcommand{\table@placement}{#1}}

\renewenvironment{table}[1][]{%
    \begingroup%
    % Insert keys from above
    \setkeys{table}{#1}%
    \newcommand{\table@internal@placement}{H}%
    \expandafter\table@default\expandafter[\table@internal@placement]%
    \ifthenelse{\isundefined{\table@toc}}{%
        % Without dedicated TOC caption
        \caption{\table@caption\label{\table@label}}%
    }{%
        % With dedicated TOC caption
        \caption[\table@toc]{\table@caption\label{\table@label}}%
    }%
    % \beginntabular%
}{%
    % \endtabular%
    \endtable@default%
    \endgroup%
}
How can I add the following customisation?

I would like to
  • 1. always center the table
    2. align the table to the "textwidth" when the table is bigger than the textwith (

    Code: Select all

    \resizebox{\textwidth}{!}{}
    ). If it is not, keep the original size (and don't stretch it)
    3. Caption at the top, centered
    4. I actually convert tables from R to TEX. I already define the caption in R. The R package "xtable" then lists the caption right before

    Code: Select all

    \end{table}
    . But in the environment that can be seen above, the caption is an input after

    Code: Select all

    \begin{table}
    . Now, I always have to cut the caption from the R output and insert it into the right place. Can I somehow automate this?
    5. add \toprule, \cmidrule, and \bottomrule
Besides what I'd like to add, I do not understand what \begingroup% and \endgroup% mean...? And \let\table@default\table...?

I know this is a lot, but any help is greatly appreciated. Thank you in advance!

Alexander

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Customise environment for table formatting

Post by Stefan Kottwitz »

Hi Alexander,

welcome to the forum!
Alexander1994 wrote:Besides what I'd like to add, I do not understand what \begingroup% and \endgroup% mean...?
That marks the beginning end the end of a group, and within a group effect of (local) commands stay local, so their effect is limited, the effect of such commands end with \endgroup.
Alexander1994 wrote:And \let\table@default\table...?
That saves the original \table macro in a macro called \table@default. The reason is, that you now can redefine \table (as done here) and still call the original \table command by the \table@default macro you made as a copy.

Can you modify your code snippet so it's compilable and we can run LaTeX on it? So we could test it and help improving it.

For example, \define@key is unknown here, I just guess it comes from the keyval package but it's not loaded here. Also, there's no example table to test. We usually like to give advice that's tested and working.

Stefan
LaTeX.org admin
Alexander1994
Posts: 7
Joined: Sat Apr 10, 2021 10:29 am

Customise environment for table formatting

Post by Alexander1994 »

Hi Stefan

Thank you for your very quick reply. It is really highly appreciated, hopefully I can give sth back once I‘m more proficient.

Beware with me, I‘m getting back to you by tomorrow morning at the latest.

Best,
Alexander
Alexander1994
Posts: 7
Joined: Sat Apr 10, 2021 10:29 am

Customise environment for table formatting

Post by Alexander1994 »

Hi Stefan,

Again, thank you for welcoming me to the forum!

Unfortunately, I did not manage to modify the snippet - the code is distributed among many different files in Overleaf. I do not have the overview of the links among them, even after trying to sort out unnecessary parts of the code, i.e. such parts whose names do not seem to have an influence to table formatting. It would me take weeks I guess... :(

That is why I just created a table in a document hoping to get some advice on how to create a "renewenvironment" based on that table... if that's possible, you are invited to reply. I would then again clarify my needs. If not, that's also fine. I know there are many tutorials out there but somehow they seem either too advanced or not really catering my needs. Here's the code:

Code: Select all

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{threeparttable}

\begin{document}

\begin{table}
	\normalsize
    \begin{center}
	\begin{tabular}{llll}
	\textbf{Data provider}          & \textbf{Category}   		    & \textbf{Subcategory}		& \textbf{Static screen}\\
	Orbis                           & Industry \& activities 	    & Industry classifications	& Selected primary codes\\
	                                & Company 					    & Status					& Active\\
	                                & Stock and Earnings estimates  & Listed/Unlisted			& Publicly listed\\
	                                & Ownership data 				& Shareholders				& Shareholders\\
	Refinitiv                       & Listing type 				    & 							& Major listing\\
	                                & Type of instrument 			& 							& Equity type\\
	\end{tabular}
	\end{center}
	\begin{tablenotes}
	    \item This is a footnote for Table 1.
	\end{tablenotes}
	\caption{My first table}
    \label{tab:table1}
\end{table}
\end{document}
The only thing I have to say is that my template uses the following document class:

Code: Select all

\documentclass[
	language=english,
	type=master, % set to bachelor, master or seminar
]{isthesis}
Yet, my quick code above uses "article". Not sure if that plays a role.

Happy weekend!

Best regards,
Alexander
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Customise environment for table formatting

Post by Stefan Kottwitz »

Hi Alexander,

before (possibly) automating, one may format and test with a single table that contains the required settings. Once that is clear, one may transfer it to a global table rededinition.

Would this be a good start?

Code: Select all

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{threeparttable}
\usepackage{adjustbox}
\usepackage{blindtext}
\usepackage{booktabs}
\usepackage[justification=centering]{caption}
\begin{document}
\noindent\blindtext% just to see the text width
\begin{table}[htbp!]
	\normalsize
	\centering
	\caption{My first table}
    \label{tab:table1}
	\begin{adjustbox}{max width=\textwidth}
		\begin{tabular}{llll}
		\toprule
		\textbf{Data provider}          & \textbf{Category}   		    & \textbf{Subcategory}		& \textbf{Static screen}\\
		\midrule
		Orbis                           & Industry \& activities 	    & Industry classifications	& Selected primary codes\\
		                                & Company 					    & Status					& Active\\
		                                & Stock and Earnings estimates  & Listed/Unlisted			& Publicly listed\\
		                                & Ownership data 				& Shareholders				& Shareholders\\
		Refinitiv                       & Listing type 				    & 							& Major listing\\
		                                & Type of instrument 			& 							& Equity type\\
		\bottomrule
		\end{tabular}
	\end{adjustbox}
	\begin{tablenotes}
	    \item This is a footnote for Table 1.
	\end{tablenotes}
\end{table}
\blindtext
\end{document}
Click on "Run LaTeX here" to see the output.

Stefan
LaTeX.org admin
Alexander1994
Posts: 7
Joined: Sat Apr 10, 2021 10:29 am

Customise environment for table formatting

Post by Alexander1994 »

Hi Stefan

The output is very decent, in my thesis I just trimmed the "midlines" a bit, but that's a detail. So your suggestion is what I basically have now in my thesis.

In other words, these table settings are
  • 1. center table or resize it to textwidth if bigger than textwidth
    2. caption above the table, centered
    3. tablenotes as wide as table, with no indent on the left, always 2pt smaller in size than the text (in my case 12pt)
While I feel this is rather easily implemented, as I already hinted at, the output of the statistical software R puts the caption below the table. Can I somehow tell Latex that whenever that is the case, please move it to the top as in our example here?

In the end, ideally, I just use begin and end table (if I create my own table) and then directly start with the first line of the table. If I use one from R, there should be the added function that it moves the caption to the top.

Best,
Alexander
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Customise environment for table formatting

Post by Stefan Kottwitz »

Forcing captions to be at the top of a table can be easy:

Code: Select all

\usepackage{float}
\floatstyle{plaintop}
\restylefloat{table}
Witht the full table:

Code: Select all

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{threeparttable}
\usepackage{adjustbox}
\usepackage{blindtext}
\usepackage{booktabs}
\usepackage[justification=centering]{caption}
\usepackage{float}
\floatstyle{plaintop}
\restylefloat{table}
\begin{document}
\noindent\blindtext% just to see the text width
\begin{table}[htbp!]
	\normalsize
	\centering
	\begin{adjustbox}{max width=\textwidth}
		\begin{tabular}{llll}
		\toprule
		\textbf{Data provider}          & \textbf{Category}   		    & \textbf{Subcategory}		& \textbf{Static screen}\\
		\midrule
		Orbis                           & Industry \& activities 	    & Industry classifications	& Selected primary codes\\
		                                & Company 					    & Status					& Active\\
		                                & Stock and Earnings estimates  & Listed/Unlisted			& Publicly listed\\
		                                & Ownership data 				& Shareholders				& Shareholders\\
		Refinitiv                       & Listing type 				    & 							& Major listing\\
		                                & Type of instrument 			& 							& Equity type\\
		\bottomrule
		\end{tabular}
	\end{adjustbox}
	\begin{tablenotes}
	    \item This is a footnote for Table 1.
	\end{tablenotes}
	\caption{My first table}
    \label{tab:table1}
\end{table}
\blindtext
\end{document}
Stefan
LaTeX.org admin
Alexander1994
Posts: 7
Joined: Sat Apr 10, 2021 10:29 am

Customise environment for table formatting

Post by Alexander1994 »

Hi Stefan

That's great news! Awesome!!

The following code (with some parts added for it to be compilable) is a typical output by R. As you can the see, the caption and label are right before "\end{table}".

Code: Select all

\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
% latex table generated in R 4.0.4 by xtable 1.8-4 package
% Sun Apr 11 09:52:41 2021
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrrr}
  \hline
 & \textbf{Life insurer} & \textbf{Health insurer} & \textbf{P\&C insurer} & \textbf{Life and P\&C insurer} & \textbf{L\&H and P\&C insurer} & \textbf{Reinsurer} & \textbf{Sum} \\ 
  \hline
\textit{2002} & 11 & 6 & 200 & 6 & 0 & 10 & 227 \\ 
  \textit{2003} & 11 & 9 & 203 & 6 & 0 & 10 & 233 \\ 
  \textit{2004} & 11 & 9 & 206 & 6 & 0 & 10 & 236 \\ 
  \textit{2005} & 11 & 10 & 209 & 6 & 0 & 10 & 240 \\ 
  \textit{2006} & 12 & 10 & 216 & 7 & 0 & 10 & 248 \\ 
  \textit{2007} & 13 & 10 & 222 & 7 & 0 & 10 & 255 \\ 
  \textit{2008} & 13 & 10 & 228 & 7 & 0 & 10 & 261 \\ 
  \textit{2009} & 13 & 10 & 230 & 7 & 0 & 10 & 263 \\ 
  \textit{2010} & 13 & 11 & 234 & 7 & 0 & 11 & 269 \\ 
  \textit{2011} & 14 & 11 & 236 & 8 & 0 & 11 & 272 \\ 
  \textit{2012} & 14 & 11 & 236 & 10 & 0 & 11 & 272 \\ 
  \textit{2013} & 14 & 11 & 239 & 10 & 0 & 11 & 275 \\ 
  \textit{2014} & 14 & 11 & 239 & 10 & 0 & 11 & 275 \\ 
  \textit{2015} & 14 & 11 & 239 & 10 & 0 & 11 & 275 \\ 
  \textit{2016} & 14 & 12 & 240 & 11 & 0 & 11 & 277 \\ 
  \textit{2017} & 15 & 12 & 241 & 11 & 0 & 11 & 279 \\ 
  \textit{2018} & 15 & 12 & 243 & 11 & 0 & 11 & 281 \\ 
  \textit{2019} & 15 & 12 & 243 & 11 & 0 & 11 & 281 \\ 
  \textit{2020} & 15 & 12 & 243 & 11 & 0 & 11 & 281 \\ 
   \hline
\end{tabular}
\caption{This is the caption} 
\label{tab1}
\end{table}
% Code from R until line 35
\end{document}
So I wonder how I can change this table's structure to the one defined before with a simple command/environment? Since - ideally - I'd like not to always enter e.g. "toprule" etc.

Best,
Alexander
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Customise environment for table formatting

Post by Ijon Tichy »

Just two note:
  • float with its command \restylefloat is one of the packages from hell. Ask the author of package caption or the author of KOMA-Script, if you don't believe me. To place the caption above the table better use \caption above tabular. If you need highly configurable tables better use a package like ctable with a completely different table syntax. BTW: At last DANTE meeting there was a very interesting submission about generating tables with CSS like syntax.
  • Do not use automatic resize of table. This results not only in ugly resized random font sizes but also resizes the thickness of all table lines. So you should always design you tables in a proper size or, e.g., rotate your tables if they are to wide.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Alexander1994
Posts: 7
Joined: Sat Apr 10, 2021 10:29 am

Customise environment for table formatting

Post by Alexander1994 »

Thank you for your notes. A quick documentation check of ctable looks nice, but seems complicated for a beginner like me. But I acknowledge your expert reply! Regarding "resize", this makes sense, too. I will decide on a case-by-case basis if the table is too small and consider rotating it when deemed too small.

No matter "table" or "ctable", I hope I can get some info regarding creating an environment that allows me to not always write the same code in each table.
Post Reply