Graphics, Figures & TablesCustomise environment for table formatting

Information and discussion about graphics, figures & tables in LaTeX documents.
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 »

Alexander1994 wrote:I hope I can get some info regarding creating an environment that allows me to not always write the same code in each table.
In any case, if you see that you repeat code, put it into a macro and use that. You can adjust the macro later at any time.

For a perfect thesis, some manual work is probably also ok. ;-) But yes, if possible use macros or (re)define an environment. Tables (floats) are just fundamental and special, and tabular environments are too.

Stefan
LaTeX.org admin

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

Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Customise environment for table formatting

Post by Ijon Tichy »

One more note: From 2020-10-01 LaTeX provides a new hook mechanism, that also allows to add code at the beginning of an environment. See lthooks-doc.pdf in an up-to-date LaTeX installation for more information. Unfortunately it cannot be used to add code to floats, unless you patch such an hook into the code of floats. Here is an example, how this can be used to automatically center figures but not tables:

Code: Select all

\documentclass{article}
\usepackage{mwe}
\usepackage{xpatch}
\NewHook{float/begin}
\makeatletter
\xapptocmd{\@floatboxreset}
          {\UseHook{float/begin}}
          {\typeout{new hook `float/begin' activated}}
          {\errmessage{cannot activate new hook `float/begin'}}
\makeatother

\AddToHook{env/figure/before}{\AddToHook{float/begin}[figure]{\centering}}
\AddToHook{env/figure/after}{\RemoveFromHook{float/begin}[figure]}
\begin{document}
\section{Test}
\begin{figure}
  \includegraphics[width=.5\textwidth]{example-image}
  \caption{automatic centering of figures}
\end{figure}

\end{document}
BTW: Do not use the hook mechanism to change tabular. Also do not redefine tabular. tabular is a generic environment that is used internally at several places average users would not expect, e.g., to typeset the author(s) using \maketitle.
Last edited by Ijon Tichy on Mon Apr 12, 2021 8:24 am, edited 2 times in total.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
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 »

Very good example for the new hooks feature!

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

Customise environment for table formatting

Post by Alexander1994 »

Thank you both for your replies - and sorry for my delay.

@Stefan: I will surely use manual inputs. Just that I get it right, creating macros can be done with "\renewenvironment" to adjust tables. Let's say I want that always all text in all columns is left-aligned, then I probably should do it in the "\renewenvironment", correct?
I failed to find another post regarding macros in your page's subsection "Graphs & Co.". Anything recommendable?

@Ijon: Will hopefully understand that later ;)
Post Reply