8

I am adding figures to a work and most of the figures are too wide and are thus not centered but are shifted to the right. I searched here and found a solution, that would consist in adding the \centerline{} command into the figure environment. This work fine for figures added with the command \includegraphics{}, like in this example:

\begin{figure}[h!]
\begin{center}
\centerline{\includegraphics{SOIR_optics}}
\end{center} % is this really useful?
\end{figure}

But when I try to add a tikzpicture instead, this solution does not work (see example)

\begin{figure}[h!]
\begin{center}
\centerline{\begin{tikzpicture} 
some code 
\end{tikzpicture}}
\end{center}
\end{figure}

My question is : are there alternatives to this \centerline{} command?

Thanks!

2
  • Is your picture wider than the margins of the page?
    – Joseph Wright
    Commented Mar 4, 2014 at 8:23
  • Yes, for some of them
    – mwoua
    Commented Mar 4, 2014 at 8:39

2 Answers 2

17

The \centerline command should never be used in a LaTeX document (unless you know precisely what you're doing, and probably only in the preamble for some definition). Use

\begin{figure}[htp]
\centering

<whatever>

\end{figure}

and <whatever> (a graphic, a TikZ picture or anything) will be centered.

15
  • 6
    This does not work for figures slightly larger than the margins :/
    – mwoua
    Commented Mar 4, 2014 at 8:39
  • @mwoua Why don't you simply scale down the image to fit the textwidth? That would make more sense...
    – jub0bs
    Commented Mar 4, 2014 at 8:57
  • 1
    If I scale it down, some details could be unreadable.
    – mwoua
    Commented Mar 4, 2014 at 9:00
  • 1
    @mwoua If you need that, enclose the object to be scaled in \makebox[\columnwidth]{...}
    – egreg
    Commented Mar 4, 2014 at 9:26
  • 4
    @egreg, If I'm not so wrong (actually I'm almost certain), you mentioned not to use \centerline in an other question as well. What is so devil about it?
    – Pouya
    Commented Mar 4, 2014 at 17:22
3

You can use adjustbox

\documentclass{scrreprt}
\usepackage[export]{adjustbox}  %%  export option makes adjustbox --
                                %%  -- goodies available inside includegraphics command
\usepackage{graphicx}

\begin{document}
X\hrulefill X
\begin{figure}[htp]
\includegraphics[width=1.1\textwidth,center]{example-image-a}
\end{figure}
\end{document}

enter image description here

This is very useful for figures that are wider than \textwidth. Another useful macro will be adjustbox environment with max width option.

\documentclass{scrreprt}
\usepackage[export]{adjustbox}
\usepackage{graphicx}

\begin{document}
X\hrulefill X

This is resized to \verb|1.1\textwidth|
\begin{figure}[htp]
\begin{adjustbox}{center,max width=1.1\textwidth}
\includegraphics[width=1.2\textwidth,center]{example-image-a}
\end{adjustbox}
\end{figure}
\clearpage
The following is not resized:
\begin{figure}[htp]
\begin{adjustbox}{center,max width=1.1\textwidth}
\includegraphics[width=0.7\textwidth,center]{example-image-b}
\end{adjustbox}
\end{figure}

\end{document}

enter image description here

enter image description here

The advantage of max width is that the content is resized only if it exceeds the max width otherwise not.

With tikzpicture environment

I assume that your tikz picture are saved as separate files. Then using adjustbox and tikzscale packages:

\documentclass{article}

\usepackage{graphicx,tikz,tikzscale}
\usepackage{adjustbox}
\usepackage{filecontents}
\begin{filecontents*}{myfig.tikz}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt]
    \draw[use as bounding box](-20,-20) rectangle (20,20);
    \node at (0,0) (A) {A};
    \node[above right] (B) at (A.north east) {B};
    \draw (A.south west)--(B.north east);
  \end{tikzpicture}
\end{filecontents*}

\begin{document}
X\hrulefill X

This is resized to \verb|1.1\textwidth|
\begin{figure}[htp]
\begin{adjustbox}{max width=1.1\textwidth,center}
\includegraphics[width=1.2\textwidth]{myfig.tikz}
\end{adjustbox}
\end{figure}
\clearpage
The following is not resized:
\begin{figure}[htp]
\begin{adjustbox}{max width=1.1\textwidth,center}
\includegraphics[width=0.7\textwidth]{myfig.tikz}
\end{adjustbox}
\end{figure}
\end{document}

enter image description here

3
  • Thanks ! This works for \includegraphics. But for a tikzpicture environment?
    – mwoua
    Commented Mar 4, 2014 at 9:16
  • 1
    @mwoua You can use tikzscale package. Give me a minute. I will provide an example.
    – user11232
    Commented Mar 4, 2014 at 9:19
  • I will not use it now but I keep it in mind. Thank you so much for your answer!
    – mwoua
    Commented Mar 4, 2014 at 9:33

You must log in to answer this question.

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