TikZ:如何将复杂的 TikZ 图像的宽度设置为 \textwidth?

TikZ:如何将复杂的 TikZ 图像的宽度设置为 \textwidth?

我有一个有点复杂的TikZ图像:

截屏


如您所见,我想在这幅TikZ图像周围放置一个边框。出于视觉和谐考虑,边框应具有完全相同的外部尺寸,如\textwidth

现在我尝试了两种不同的方法来在图像周围添加边框:

  1. 在序言中已经将边框放在了周围tikzimage:这样边框就非常靠近图像内容了,看起来不错,但它与左边缘有很大的边距\textwidth。在图像中,这显示为内边框。

  2. tikz将召回函数周围的边框放在document-part 内:这会导致图像内容和边框之间有很大空间,但至少左边框与左\textwidth边缘相吻合。在图像中,这显示为外边框。

正如你所见,它们都不能正常工作。

如何正确设置图像的大小以及边框?我希望在以后更改文档几何形状时能够灵活应对,因此它应该自动适应\textwidth


最小工作示例(MWE):

\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}

\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}

非常感谢!

答案1

如果您的图片比您需要的宽度更宽,\textwidth则必须对其进行调整以使其达到所需的宽度。除非您进行调整,否则此解决方案不会更改任何图片尺寸(宽度、高度、字体大小)。

另一个解决方案是使用standalone类。在这种情况下,您将处理一个独立的文件来生成所需的图像,然后使用\includegraphics带有选项的命令将其包含在文本中width=\textwidth。在这种情况下,图像尺寸将缩放(文本也是如此)以填充\textwidth

以您的 MWE 为例。插入文档wheelchartwithlegendstandalonetikz选项将裁剪最终结果。tikzpicture选项show background rectangle将绘制所需的边框。

%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}

在此处输入图片描述

现在,您可以将此图像包含在带有包的文本中graphicx。第一张图像显示其真实大小,第二张图像调整为\textwidth

\documentclass{article}
\usepackage{blindtext}
\usepackage{graphicx}

\begin{document}

\blindtext

\begin{figure}
\includegraphics{wheel}

\includegraphics[width=\textwidth]{wheel}
\end{figure}

\end{document}

在此处输入图片描述

答案2

首先,您的图片前后有一些不必要的空格。这就是导致 es 之间左右两侧有额外距离的原因\fbox。我删除了空行(大多数空行无关紧要,但只是为了安全起见)并添加了一些%后行以消除空格。然后,您的图片仍然太宽,但由于不是太多,可以通过缩放来修复,这里是\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}

在此处输入图片描述

相关内容