5

I have a lot of tikzpicture elements in my document. I want all the elements to be centered automatically without writing \begin{center}...\end{center}. I found a solution to make a figure center by the following code:

\documentclass{memoir}

\usepackage[draft]{graphicx}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

% Solution 1 <--- Best for figures
\setfloatadjustment{figure}{\centering}

% Solution 2 <--- does not work
% \makeatletter
% \g@addto@macro\@floatboxreset{\centering}
% \makeatother

% % Solution 3 --- works
% \let\origfigure\figure
% \let\endorigfigure\endfigure
% \renewenvironment{figure}[1][tbph]{%
%     \origfigure[#1]%
%     \centering
% }{%
%     \endorigfigure
% }

\begin{document}

The first paragraph.

\begin{figure}  
    \includegraphics{a.jpg}
    \caption[Long caption]{Caption}
    \label{pic-a}
\end{figure}

And the second.


\begin{tikzpicture}
    \node[rectangle,
        draw = blue,
        text = olive,
        fill = gray!30,
        minimum width = 5cm, 
        minimum height = 1cm] (r) at (0,0) {Rectangle};
    
\end{tikzpicture}
 
\begin{tikzpicture}
 
\node[isosceles triangle,
    draw,
    fill=cyan!30,
    minimum size =3cm] (T) at (0,0) {};
 
\end{tikzpicture}

\end{document}

However, my tikzpicture elements are not effected. How can I do that (to set all the tikzpictures to be centered automatically without using \begin{center}...\end{center} each time?

1
  • Looks like that there isn't any built in way, you can define your own environment that creates a centered tikzpicture.
    – user202729
    Dec 8, 2021 at 16:28

2 Answers 2

6

It seems likely that you may not want every tikzpicture to be centered. One option is to create a new environment:

\documentclass{memoir}

\usepackage{tikz}

\newenvironment{centikz}{\begin{center}\begin{tikzpicture}}{\end{tikzpicture}\end{center}}

\begin{document}

An inline tikzpicture \begin{tikzpicture}\draw(0,0) node[fill, inner sep=3pt]{} to (1,0) node[fill, inner sep=3pt]{};\end{tikzpicture} 
should not be centered, using either begin/end notation or \tikz{\draw(0,0) node[fill, inner sep=3pt]{} to (1,0) node[fill, inner sep=3pt]{};} shortened notation. 
But sometimes \begin{centikz}\draw(0,0) node[fill, inner sep=3pt]{} to (1,0) node[fill, inner sep=3pt]{};\end{centikz} 
you want it that way.

\end{document}

enter image description here

4

You can centre all tikzpicture environments using

\documentclass{memoir}
\usepackage{etoolbox}
\usepackage{tikz}
\BeforeBeginEnvironment{tikzpicture}{\begin{center}}
\AfterEndEnvironment{tikzpicture}{\end{center}}
\begin{document}
    \begin{tikzpicture}
        \node {Test};
    \end{tikzpicture}
\end{document}

Here

\BeforeBeginEnvironment{environment}{code before}

and

\AfterEndEnivronment{environment}{code after}

prepend and append code before and code after to \begin{environment} and \end{environment}. See the etoolbox documentation for more details.

Note that if you are using memoir or a late enough version of LaTeX you may not need the etoolbox package for this to work.

4
  • I am using memoir class. @Willoughby
    – mmr
    Dec 8, 2021 at 16:47
  • 2
    Clever solution. I have to dig into the etoolbox package, it seems to be very useful. +1
    – SebGlav
    Dec 8, 2021 at 17:12
  • @Willoughboy Nice, +1. You tickled my curiosity. Question to follow (for you?) with Beamer.
    – JeT
    Dec 8, 2021 at 20:21
  • @Willoughboy here it is
    – JeT
    Dec 8, 2021 at 20:33

You must log in to answer this question.

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