TikZ: How to set width of complicated TikZ-image to \textwidth?
If your image is wider than \textwidth
you'll have to work on it to adjust it to desired width. This solution won't change any image dimension (width, height, font size) unless you do it.
Another solution could be to use standalone
class. In this case, you'll work on an independent file to produce the desired image and, later on, include it in your text with an \includegraphics
command whit width=\textwidth
option. In this case, the image dimensions will be scaled (also text) to fill \textwidth
.
As an example with your MWE. Insert wheelchartwithlegend
inside a standalone
document. tikz
option will crop the final result. And tikzpicture
option show background rectangle
will draw the desired border.
%File wheel.tex
\documentclass[tikz]{standalone}
\usepackage{tikz}
%\usepackage{blindtext}
\usetikzlibrary{arrows.meta, backgrounds}
%\usepackage[most]{tcolorbox}
% Adjusts the size of the wheel:
\def\innerradius{0\textwidth}
\def\outerradius{0.15\textwidth}
\definecolor{A0}{HTML}{A4DA90}
\definecolor{B0}{HTML}{ECEB80}
\definecolor{C0}{HTML}{9D7AB3}
\definecolor{D0}{HTML}{7E8BB4}
% The main macro
\newcommand{\wheelchartwithlegend}[1]{
% Calculate total
\pgfmathsetmacro{\totalnum}{0}
\foreach \value/\colour/\name in {#1} {
\pgfmathparse{\value+\totalnum}
\global\let\totalnum=\pgfmathresult
}
\begin{tikzpicture}[show background rectangle]
% Calculate the thickness and the middle line of the wheel
\pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
\pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}
% Rotate so we start from the top
\begin{scope}[rotate=90]
% add coordinate to define the upper left starting point of the legend entries
\coordinate (L-0) at (\outerradius+0mm,-\outerradius-2.5cm);
% Loop through each value set. \cumnum keeps track of where we are in the wheel
\pgfmathsetmacro{\cumnum}{0}
\foreach [count=\i,remember=\i as \j (initially 0)] \value/\colour/\name in {#1} {
\pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}
% Calculate the percent value
% \pgfmathsetmacro{\percentage}{\value/\totalnum*100}
\pgfmathsetmacro{\percentage}{\value}
% Calculate the mid angle of the colour segments to place the labels
\pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}
% This is necessary for the labels to align nicely
\pgfmathparse{
(-\midangle<180?"west":"east")
} \edef\textanchor{\pgfmathresult}
\pgfmathsetmacro\labelshiftdir{1-2*(-\midangle>180)}
% Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
\fill[\colour] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
(-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;
% Draw the data labels
\draw [Circle-,thin] node [append after command={(\midangle:\midradius pt) -- (\midangle:\outerradius + 1ex) -- (\tikzlastnode)}] at (\midangle:\outerradius + 1ex) [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\pgfmathprintnumber{\percentage}\thinspace\%};
% add legend node
\node [anchor=north west,text width=5cm,font=\footnotesize] (L-\i) at (L-\j.south west) {\name};
% draw legend image
\fill [fill=\colour] ([xshift=-3pt,yshift=1mm]L-\i.north west) rectangle ++(-2mm,5mm);
% Set the old cumulated angle to the new value
\global\let\cumnum=\newcumnum
}
\end{scope}
\end{tikzpicture}
% Closing \fbox
} % Closing \newenvironment
\begin{document}
\wheelchartwithlegend{
54.52/D0/{Organic (kitchen and garden waste)},
21.23/C0/{Plastics},
13.04/B0/{Textiles (leather, sanitary, diaper)},
11.21/A0/{Others}
}
\end{document}
Now you can include this image in your text with graphicx
package. First image shows its real size and the second one is adjusted to \textwidth
.
\documentclass{article}
\usepackage{blindtext}
\usepackage{graphicx}
\begin{document}
\blindtext
\begin{figure}
\includegraphics{wheel}
\includegraphics[width=\textwidth]{wheel}
\end{figure}
\end{document}
First of all, you have some unwanted spaces before and after your picture. This is what causes the extra distances to the right and left between the \fbox
es. I have removed the empty rows (most of them does not matter, but just to be on the safe side) and added some %
after rows to get rid of spaces. Then, your picture is still too wide, but since it is not too much, it can be fixed with scaling, here by \resizebox
.
\documentclass{article}
\usepackage{tikz}
\usepackage{blindtext}
\usetikzlibrary{arrows.meta}
% Adjusts the size of the wheel:
\def\innerradius{0\textwidth}
\def\outerradius{0.15\textwidth}
\definecolor{A0}{HTML}{A4DA90}
\definecolor{B0}{HTML}{ECEB80}
\definecolor{C0}{HTML}{9D7AB3}
\definecolor{D0}{HTML}{7E8BB4}
% The main macro
\newcommand{\wheelchartwithlegend}[1]{%
% Calculate total
\pgfmathsetmacro{\totalnum}{0}%
\foreach \value/\colour/\name in {#1} {%
\pgfmathparse{\value+\totalnum}
\global\let\totalnum=\pgfmathresult
}%
\fbox{\begin{tikzpicture}
% Calculate the thickness and the middle line of the wheel
\pgfmathsetmacro{\wheelwidth}{\outerradius-\innerradius}
\pgfmathsetmacro{\midradius}{(\outerradius+\innerradius)/2}
% Rotate so we start from the top
\begin{scope}[rotate=90]
% add coordinate to define the upper left starting point of the legend entries
\coordinate (L-0) at (\outerradius+0mm,-\outerradius-2.5cm);
% Loop through each value set. \cumnum keeps track of where we are in the wheel
\pgfmathsetmacro{\cumnum}{0}
\foreach [count=\i,remember=\i as \j (initially 0)] \value/\colour/\name in {#1} {
\pgfmathsetmacro{\newcumnum}{\cumnum + \value/\totalnum*360}
% Calculate the percent value
% \pgfmathsetmacro{\percentage}{\value/\totalnum*100}
\pgfmathsetmacro{\percentage}{\value}
% Calculate the mid angle of the colour segments to place the labels
\pgfmathsetmacro{\midangle}{-(\cumnum+\newcumnum)/2}
% This is necessary for the labels to align nicely
\pgfmathparse{
(-\midangle<180?"west":"east")
} \edef\textanchor{\pgfmathresult}
\pgfmathsetmacro\labelshiftdir{1-2*(-\midangle>180)}
% Draw the color segments. Somehow, the \midrow units got lost, so we add 'pt' at the end. Not nice...
\fill[\colour] (-\cumnum:\outerradius) arc (-\cumnum:-(\newcumnum):\outerradius) --
(-\newcumnum:\innerradius) arc (-\newcumnum:-(\cumnum):\innerradius) -- cycle;
% Draw the data labels
\draw [Circle-,thin] node [append after command={(\midangle:\midradius pt) -- (\midangle:\outerradius + 1ex) -- (\tikzlastnode)}] at (\midangle:\outerradius + 1ex) [xshift=\labelshiftdir*0.5cm,inner sep=0pt, outer sep=0pt, ,anchor=\textanchor]{\pgfmathprintnumber{\percentage}\thinspace\%};
% add legend node
\node [anchor=north west,text width=5cm,font=\footnotesize] (L-\i) at (L-\j.south west) {\name};
% draw legend image
\fill [fill=\colour] ([xshift=-3pt,yshift=1mm]L-\i.north west) rectangle ++(-2mm,5mm);
% Set the old cumulated angle to the new value
\global\let\cumnum=\newcumnum
}
\end{scope}
\end{tikzpicture}%
}% Closing \fbox
}% Closing \newenvironment
\begin{document}
\blindtext
\begin{figure}
\resizebox{\linewidth}{!}{%
\fbox{\wheelchartwithlegend{
54.52/D0/{Organic (kitchen and garden waste)},
21.23/C0/{Plastics},
13.04/B0/{Textiles (leather, sanitary, diaper)},
11.21/A0/{Others}
}%
}%
}
\end{figure}
\end{document}