[Tex/LaTex] Scaling a tikzpicture in the figure environment with caption

captionsfloatsscalingtikz-pgf

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

?

Best Answer

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

Related Question