3

I want to scale a tikzpicture within the figure environment. The code that produces the scaled figure:

\documentclass[11pt,a4paper]{article}
\usepackage{ifthen}
\usepackage{tikz}
\usepackage{tikzscale}
\tikzstyle{ball} = [circle,shading=ball, ball color=blue!60!white]
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[scale=0.5]
\pgflowlevelsynccm
\foreach \a in {1,...,6}
    {
    \node[style=ball] (r\a) at (1.5*\a,0) {\textcolor{yellow}{$\mu_\a$}};
    \draw[very thick] (r\a)--(1.5*\a,1.75);
    \draw[very thick] (r\a)--(1.5*\a,-1.75);
    \ifthenelse{\a = 6}
        {}
        {\draw[very thick] (r\a)--(1.5+1.5*\a,0);}
    }
\draw[line width=5mm,draw=green] (0.5,-1) rectangle (10,1);
\end{tikzpicture}
\caption{M1} \label{fig:M1}
\end{figure}
\end{document}

This produces a tikz picture that is correctly scaled, but now overlaps with the caption:

Is there any way around this?

Also, is it possible to rescale the tikzpicture with respect to the textwidth with something like

0.5\textwidth

?

5

For rescaling in general you can use a resizebox (from the graphicx package), which also works for tikzpicture. The first argument is the width, the second the height, or ! to scale proportionally. With a resizebox you don't need coordinate transformation, which seems to cause the overlap in your example.

MWE:

\documentclass[11pt,a4paper]{article}
\usepackage{ifthen}
\usepackage{tikz}
%\usepackage{tikzscale}
\tikzstyle{ball} = [circle,shading=ball, ball color=blue!60!white]
\begin{document}
\begin{figure}
\centering
\resizebox{0.3\textwidth}{!}{
\begin{tikzpicture}
%\pgflowlevelsynccm
\foreach \a in {1,...,6}
    {
    \node[style=ball] (r\a) at (1.5*\a,0) {\textcolor{yellow}{$\mu_\a$}};
    \draw[very thick] (r\a)--(1.5*\a,1.75);
    \draw[very thick] (r\a)--(1.5*\a,-1.75);
    \ifthenelse{\a = 6}
        {}
        {\draw[very thick] (r\a)--(1.5+1.5*\a,0);}
    }
\draw[line width=5mm,draw=green] (0.5,-1) rectangle (10,1);
\end{tikzpicture}
}
\caption{M1} \label{fig:M1}
\end{figure}
\end{document}

Result:

enter image description here

3
  • Thanks for the quick reply! This solution works beautifully and solves all my problems! Apr 9 '17 at 13:29
  • 1
    @GrimReaper, scaling is evil. Now you have text in image so small that it can be read only with magnifying glass ... is this really what you want?
    – Zarko
    Apr 9 '17 at 13:35
  • @Zarko , thanks for the heads up! In this case, fortunately for me, this figure is a generalization of a larger one in my document. Thanks for the tip though! Apr 9 '17 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.