缩小 tikz 图片的尺寸

缩小 tikz 图片的尺寸

我有一个使用 tikz 创建的流程图。

例子,

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{backgrounds,
                fit,
                positioning}

\begin{document} 
    \begin{tikzpicture}[
 node distance = 4mm and 4mm,
   base/.style = {rectangle, draw, align=center}, 
  steps/.style = {base, rounded corners, fill=gray!50, 
                  minimum height=1cm, text width=3cm},
process/.style = {base,
                  minimum height=2cm, text width=4cm, fill=white}
                  ]
\node (process1) [process]                      {text text};
\node (process2) [process, right=of process1]   {text text
    text text text text text text text text text text text text text text text text};
\node (process3) [process, right=of process2]   {text text text};
\node (step1) [steps, above=of process1]                   {Step 1};
\node (step2) [steps, above=of process2]   {Step 2};
\node (step3) [steps, above=of process3]   {Step 3};
%
\scoped[on background layer]
{
\node[fit=(step1) (process1), fill=red!20] {};
\node[fit=(step2) (process2), fill=red!40] {};
\node[fit=(step3) (process3), fill=red!60] {};
}
    \end{tikzpicture}
\end{document}

有人能告诉我如何将其中创建的图形的尺寸\begin{tikzpicture}..\end{tikzpicture}缩小到原始尺寸的 0.8 吗?

编辑:我尝试了 scale=0.8,但由于某种原因它没有起作用。

EDIT2:我尝试了下面的方法,但是 scalebox 和 scale 都不起作用

\documentclass[]{article}
\usepackage{algorithmic}
\usepackage{graphicx}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric,
                arrows,
                positioning,
                arrows.meta,
                backgrounds,
                fit,
                matrix}

% image
\usepackage{svg}
\usepackage{subfigure}
\usepackage{xcolor}
\usepackage{booktabs} 
\usepackage{multirow}
\usepackage{siunitx}


% ----------------------------------------------------------------------------

\begin{document}


 \begin{figure*}[!t]
\centering
% \scalebox{0.5}{

\begin{tikzpicture}[xscale=0.5, yscale=0.5,transform shape,
    base/.style = {rectangle, draw, inner sep=2mm, align=center}, 
  steps/.style = {base, rounded corners, fill=gray!50, 
                  minimum height=1cm, text width=24mm},    % changed
 process/.style = {base,
                  minimum height=3cm, text width=34mm,     % changed 
                  fill=white},
 decision/.style  = {diamond, minimum height=1cm ,
                    draw=black, fill=white,text width=24mm,
                    inner sep=2mm, align = center},
 img/.style={inner sep=0pt,execute at begin node={%
    \includegraphics[width=4cm,height=4cm]{#1}}},%added
  >=Stealth 
                  ]
\matrix[matrix of nodes,column sep=1em,row sep=1em,nodes in empty cells,
nodes={anchor=center},
row 1/.style={nodes=steps,execute at begin node={Step \the\pgfmatrixcurrentcolumn}},
row 6/.style={nodes={text width=9em,align=center}}]
(mat) {
 & &  \\
 |[process]| text1 & 
 |[process]| text2 &
 |[process]| text3\\ 
 |[img=im]| & |[img=im]| & |[img=im]|  \\[1em]
 |[img=im]| & |[process]| & |[img=im]|  \\[1em]
 |[img=im]| & |[img=im]| & |[img=im]|  \\[-1em]
  &  &  \\
};
\scoped[on background layer]
{\foreach \X [count=\Y] in {black!2,black!5,black!15} {
 \node[fit=(mat-1-\Y) (mat-2-\Y) (mat-3-\Y) (mat-4-\Y) (mat-5-\Y) (mat-6-\Y), fill=\X]{};
}
}
\path[-Stealth]
(mat-2-1) edge (mat-2-2) 
(mat-2-2) edge (mat-2-3)
(mat-3-1) edge (mat-4-1)
(mat-4-1) edge (mat-5-1)
(mat-5-1) edge (mat-5-2)
(mat-5-2) edge (mat-4-2)
(mat-4-2) edge (mat-3-2)
(mat-3-2) edge (mat-3-3)
(mat-3-3) edge (mat-4-3)
(mat-4-3) edge (mat-5-3);
\end{tikzpicture}
% }
\end{figure*}
\end{document}

答案1

如果要在包含此类图形时控制其大小,则可以使用standalonetikzscale将示例代码(此处保存在figure.tex)作为使用常规的图形包含\includegraphics。请参阅下面的示例:

\documentclass{article}

\usepackage{tikz}
\usepackage{standalone} % Necessary to skip headers of {standalone} documents
\usepackage{tikzscale} % To use \includegraphics with TikZ code
\usetikzlibrary{backgrounds,fit,positioning}

\begin{document}

\includegraphics[width=1\linewidth]{figure.tex}

\includegraphics[width=0.8\linewidth]{figure.tex}

\includegraphics[width=0.4\linewidth]{figure.tex}

\end{document}

输出结果如下: 任意给定规模的包容性示例

答案2

现在必须跑了,@Archange 的回答更好!

针对新版本问题的新答案:请不要这样做,而是提出另一个问题!

好的,图表很复杂,matrix环境使用了很多 catcodes 更改(这里是技术性),因此很难将其嵌入另一个命令中。我会这样做:

  1. 把你的图表放在一个独立的文件中,就像lalla.tex这里一样(在 overleaf 中,它可以在不同的项目中;也许你可以进入同一个项目,但我不是 overleaf 工作方式的专家(你可以问另一个问题)。因此,您有此文件,lalla.tex它是您的图片的独立文件。
\documentclass[border=3pt]{standalone}
\usepackage{algorithmic}
\usepackage{graphicx}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric,
                arrows,
                positioning,
                arrows.meta,
                backgrounds,
                fit,
                matrix}

% image
\usepackage{svg}
\usepackage{subfigure}
\usepackage{xcolor}
\usepackage{booktabs} 
\usepackage{multirow}
\usepackage{siunitx}
% ------------------
\begin{document}
\begin{tikzpicture}[
    base/.style = {rectangle, draw, inner sep=2mm, align=center}, 
  steps/.style = {base, rounded corners, fill=gray!50, 
                  minimum height=1cm, text width=24mm},    % changed
 process/.style = {base,
                  minimum height=3cm, text width=34mm,     % changed 
                  fill=white},
 decision/.style  = {diamond, minimum height=1cm ,
                    draw=black, fill=white,text width=24mm,
                    inner sep=2mm, align = center},
 img/.style={inner sep=0pt,execute at begin node={%
    \includegraphics[width=4cm,height=4cm]{#1}}},%added
  >=Stealth 
                  ]
\matrix[matrix of nodes,column sep=1em,row sep=1em,nodes in empty cells,
nodes={anchor=center},
row 1/.style={nodes=steps,execute at begin node={Step \the\pgfmatrixcurrentcolumn}},
row 6/.style={nodes={text width=9em,align=center}}]
(mat) {
 & &  \\
 |[process]| text1 & 
 |[process]| text2 &
 |[process]| text3\\ 
 |[img=im]| & |[img=im]| & |[img=im]|  \\[1em]
 |[img=im]| & |[process]| & |[img=im]|  \\[1em]
 |[img=im]| & |[img=im]| & |[img=im]|  \\[-1em]
  &  &  \\
};
\scoped[on background layer]
{\foreach \X [count=\Y] in {black!2,black!5,black!15} {
 \node[fit=(mat-1-\Y) (mat-2-\Y) (mat-3-\Y) (mat-4-\Y) (mat-5-\Y) (mat-6-\Y), fill=\X]{};
}
}
\path[-Stealth]
(mat-2-1) edge (mat-2-2) 
(mat-2-2) edge (mat-2-3)
(mat-3-1) edge (mat-4-1)
(mat-4-1) edge (mat-5-1)
(mat-5-1) edge (mat-5-2)
(mat-5-2) edge (mat-4-2)
(mat-4-2) edge (mat-3-2)
(mat-3-2) edge (mat-3-3)
(mat-3-3) edge (mat-4-3)
(mat-4-3) edge (mat-5-3);
\end{tikzpicture}
\end{document}
  1. 您创建一个 PDF(lalla.pdf或其他内容),然后在主项目中上传该 PDF,并:
\documentclass[]{article}
\usepackage{graphicx}

\begin{document}

\begin{figure}[!t]
    \centering
    \includegraphics[width=0.3\linewidth]{lalla.pdf}
    \caption{here, so small}
\end{figure}

blah blah blah
\end{document}

获得方法:

在此处输入图片描述

旧答案,针对问题的第一个版本

好吧 --- 如果您使用standalone,当您查看图形时它将始终是一整页 --- 页面大小会发生变化,但通常查看器会适合它,因此您看不到任何差异。

对我来说使用scale=0.5, transform shape有效;参见示例(仅原始示例,带有复制的tikzpicture环境和固定页面article类---是的,第一个已满):

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit, positioning}

\begin{document}
\begin{tikzpicture}[
    node distance = 4mm and 4mm,
    base/.style = {rectangle, draw, align=center},
    steps/.style = {base, rounded corners, fill=gray!50,
    minimum height=1cm, text width=3cm},
    process/.style = {base,
    minimum height=2cm, text width=4cm, fill=white}
    ]
    \node (process1) [process]                      {text text};
    \node (process2) [process, right=of process1]   {text text
        text text text text text text text text text text text text text text text text};
    \node (process3) [process, right=of process2]   {text text text};
    \node (step1) [steps, above=of process1]                   {Step 1};
    \node (step2) [steps, above=of process2]   {Step 2};
    \node (step3) [steps, above=of process3]   {Step 3};
    %
    \scoped[on background layer]
    {
        \node[fit=(step1) (process1), fill=red!20] {};
        \node[fit=(step2) (process2), fill=red!40] {};
        \node[fit=(step3) (process3), fill=red!60] {};
    }
\end{tikzpicture}

\begin{tikzpicture}[scale=0.5, transform shape,
    node distance = 4mm and 4mm,
    base/.style = {rectangle, draw, align=center},
    steps/.style = {base, rounded corners, fill=gray!50,
    minimum height=1cm, text width=3cm},
    process/.style = {base,
    minimum height=2cm, text width=4cm, fill=white}
    ]
    \node (process1) [process]                      {text text};
    \node (process2) [process, right=of process1]   {text text
        text text text text text text text text text text text text text text text text};
    \node (process3) [process, right=of process2]   {text text text};
    \node (step1) [steps, above=of process1]                   {Step 1};
    \node (step2) [steps, above=of process2]   {Step 2};
    \node (step3) [steps, above=of process3]   {Step 3};
    %
    \scoped[on background layer]
    {
        \node[fit=(step1) (process1), fill=red!20] {};
        \node[fit=(step2) (process2), fill=red!40] {};
        \node[fit=(step3) (process3), fill=red!60] {};
    }
    \end{tikzpicture}
\end{document}

在此处输入图片描述

另一种可能性是将其包含tikzpicturescalebox(来自graphicx包)中;更改第二张图片以将其包含在scalebox命令中:

\documentclass[]{article}
\usepackage{tikz}
\usepackage{graphicx}
\usetikzlibrary{backgrounds, fit, positioning}

\begin{document}
\begin{tikzpicture}[
    node distance = 4mm and 4mm,
    base/.style = {rectangle, draw, align=center},
    steps/.style = {base, rounded corners, fill=gray!50,
    minimum height=1cm, text width=3cm},
    process/.style = {base,
    minimum height=2cm, text width=4cm, fill=white}
    ]
    \node (process1) [process]                      {text text};
    \node (process2) [process, right=of process1]   {text text
        text text text text text text text text text text text text text text text text};
    \node (process3) [process, right=of process2]   {text text text};
    \node (step1) [steps, above=of process1]                   {Step 1};
    \node (step2) [steps, above=of process2]   {Step 2};
    \node (step3) [steps, above=of process3]   {Step 3};
    %
    \scoped[on background layer]
    {
        \node[fit=(step1) (process1), fill=red!20] {};
        \node[fit=(step2) (process2), fill=red!40] {};
        \node[fit=(step3) (process3), fill=red!60] {};
    }
\end{tikzpicture}

\scalebox{0.5}{
\begin{tikzpicture}[
    node distance = 4mm and 4mm,
    base/.style = {rectangle, draw, align=center},
    steps/.style = {base, rounded corners, fill=gray!50,
    minimum height=1cm, text width=3cm},
    process/.style = {base,
    minimum height=2cm, text width=4cm, fill=white}
    ]
    \node (process1) [process]                      {text text};
    \node (process2) [process, right=of process1]   {text text
        text text text text text text text text text text text text text text text text};
    \node (process3) [process, right=of process2]   {text text text};
    \node (step1) [steps, above=of process1]                   {Step 1};
    \node (step2) [steps, above=of process2]   {Step 2};
    \node (step3) [steps, above=of process3]   {Step 3};
    %
    \scoped[on background layer]
    {
        \node[fit=(step1) (process1), fill=red!20] {};
        \node[fit=(step2) (process2), fill=red!40] {};
        \node[fit=(step3) (process3), fill=red!60] {};
    }
\end{tikzpicture}
}
\end{document}

你将拥有几乎结果相同(在第一个选项中,框中的某些距离似乎fit与其余距离的比例不一样……)

在此处输入图片描述

相关内容