子图环境中的 TikZ 无法通过 \resizebox 正确缩放

子图环境中的 TikZ 无法通过 \resizebox 正确缩放

我正在尝试将两个 TikZ 图形并排放置,将它们每个缩放到半页。我目前正在使用\subfigure[t]{.5\linewidth}和进行此操作\resizebox{\linewidth}{!}{}。后者是我在另一个脚本中发现的,但我并不完全理解,更多的是我重新缩放了图形。无论如何,第二个图形变成了几条小黑线。我不知道为什么,搜索也没有给我更好的线索。有人知道我哪里做错了吗?抱歉,MWE 不是那么简单。

\documentclass[twocolumn, 10pt]{extarticle}
  \usepackage[utf8]{inputenc}
  \usepackage[english]{babel}
  \usepackage{csquotes}
  \usepackage[T1]{fontenc}
  \usepackage{amsmath}
  \usepackage[hypcap = true]{subcaption}
  \usepackage{tikz}
  \usetikzlibrary{shapes.geometric, positioning, automata, arrows, calc}

  \begin{document}
  \begin{figure*}[!hbtp]
    \tikzstyle{square}=[rectangle,thick,minimum size=0.5cm,draw=black!100,fill=blue!20]
    \begin{subfigure}[t]{.5\linewidth}
    \centering
    \resizebox{\linewidth}{!}{
        \begin{tikzpicture}[auto, outer sep=3pt, node distance=2cm,>=latex']
        \node [square, text width = .8\textwidth] (Draw) {LalaLal aLalaLal aLalaL alaLalaLalaLal aLalaLala LalaLala};           
        \node [square, below = 15mm of Draw, text width = .7\textwidth] (Matrix) {LalaLala LalaL alaLalaLalaLa laLa laLalaL alaLa laLal aLalaLala LalaL alaLal aLala};              
        \node [square, below = 12mm of Matrix] (TrialMatrix) {Experiment matrix of a subset of the trial points.};              
        \node [square, below = 15mm of TrialMatrix] (TrialPoint) {La laL al aLala Lala LalaL alaLa laLalaLalaLal aLalaLala};            
        \node [square, below = 10mm of TrialPoint] (Pnew) {LalaLalaL ala LalaL alaLal aLala LalaLa laL alaL ala Lala };             
        \node [below = 20mm of Pnew] (LogicGate) {};            
        \node [square, right = 30mm of LogicGate] (Penalty) {LalaLalaLa laLal aLalaLa laLal aLa laLala};            
        \node [square, below = 10mm of Penalty,fill=red!20] (PenaltyEnd) {LalaLalaLala};            
        \node [square, left = 15mm of LogicGate,text width = .6\textwidth] (Replace) {RLa laLalaLal aLala LalaLa laLal aLal aLala};             
        \node [square, below = 10mm of LogicGate,text width = .6\textwidth] (Criteria) {Lala LalaLala LalaLa la};           
        \draw [thick,->] (Draw.south) to node [above left] {Lal aLa laLa la Lal aLa la Lala } node [above right] {fLala La laLa laLala} node [left] {Lal aL ala LalaLal a} node [right] { Lala Lala Lala Lala} node [below left] {} node [below right] {} (Matrix.north);           
        \draw [thick,->] (Matrix.south) to node [left] {La laL ala LalaL al aLa la} node [right] {LalaL alaLa la Lala} node [below left] {Lala Lal aL alaLa la} node [below right] {Lal aLala Lala} (TrialMatrix.north);            
        \draw [thick,->] (TrialMatrix.south) to node [above left] {Lala LalaLa laLal aLala} node[above right] {LalaL alaLal aLalaLal aLalaL alaL ala La laLal a} node [left] {LalaL alaLal aLalaLal aLalaLal aLalaL alaLalaLa laLalaLa laLala} node [right] {La laL alaLalaL alaLalaL alaLalaLal aLalaL alaLala } node [below left] {La laLalaLa laLalaLa laLalaL alaLal aLala} node [below right] {random trial point. $P_{new}=2{\times}\overline{P}-P_{r}$}(TrialPoint.north);           
        \draw [thick, ->] (TrialPoint.south) -- node [left] {La la LalaLa l aLal aLa laLal aLala} node [right] {La laLal aLal aLala Lala Lala Lala} (Pnew.north);            
        \draw [thick,->] (Pnew.south) -- node [above left] {LalaL alaLal aLala LalaL alaLala} node [above right] {La laLala LalaLala} node [left] {LalaL a laL alaL ala} node [right] { Lala Lal aLala } node [below left] {Lal aLa laLala} node [below right] {La laL alaLala.} (LogicGate);           
        \draw [thick,->] (LogicGate) -- node [below] {LalaLala} (Penalty);              
        \draw [thick,->] (LogicGate) -- node [below] {} (Criteria);             
        \draw [thick,->] (LogicGate) -- node [below] {Lala Lala Lala} (Replace);            
        \draw [thick,->] (Penalty) -- node[left] {Lala} node [right] {LalaLalaLala} (PenaltyEnd);      
        \draw [thick,->] (Penalty.east) -- ($(Penalty.east) - (-.3,0)$) |- node [below left] {La Lalalala} (Matrix.east);           
        \draw [thick,->] (Criteria.west) -- node [above] {Lala} ($(Criteria.west) - (4.6,0)$) |- (Matrix.west);             
        \draw [thick,->] (Criteria.east) -- node [above] {Lqlq} (PenaltyEnd.west);              
        \end{tikzpicture}
        }
        \caption{Lala
        }\label{LalaLala}
    \end{subfigure}
  ~
    \begin{subfigure}[t]{.5\linewidth}
    \centering
    \resizebox{\linewidth}{!}{
        \begin{tikzpicture}[auto, outer sep=3pt, node distance=2cm,>=latex']            
        \node [square](CRS12) {Lala LalaLalaLala Lala};             
        \node [square, right = 30mm of CRS12] (CRS22) {LalaL alaLalaLalaLa laLalaLalaL laLala};             
        \node [square, below = 10mm of CRS12] (LSQ12) {Lal aLalaLal aLalaL alaLal aLala LalaLala.};              
        \node [square, below = 10mm of CRS22] (LSQ22) {LLa aLalaLal aLa laLal aLal aLalaala};           
        \node [square] (CRSBoth) at ($(LSQ12)!50!(LSQ22)$) {Lal LalaLal aLalaL ala LalaLala LalaLala};
        \node[square,below of=CRSBoth] {Lala LalaL alaLalaLala LalaLalaLala Lala};
        \end{tikzpicture}
    }
  \caption{Lala.}\label{Lala}
    \end{subfigure}
  \caption{LalaLala}
  \label{LalaLalaLala}
  \end{figure*}
  \end{document}

答案1

首先\linewidth是文本行的宽度。在正常的单列文本中,它与文本宽度 ( ) 相同\textwidth,但在双列中,它是一列的宽度。当您说

\begin{subfigure}[t]{.5\linewidth}
  \resizebox{\linewidth}{!}{

您首先要创建一个 的子图0.5\linewdith,即线宽的一半。在此框内,文本宽度跨越全局文本宽度的一半。也就是说,局部\linewidth是子图的宽度。因此,第二行缩放您的 tikzpicture 以适合框。

黑框不仅仅是黑框,它们是您的节点。我不确定您想用 做什么($(LSQ12)!50!(LSQ22)$),但效果是节点(和(CRSBoth))在右边很远。尝试只写图片而不写\resizebox,并将图片放在 中\fbox,您会看到它继续延伸到纸张外面。我将 改为!50!+然后看起来好多了(可能不是您想要的,但好多了:)。

最后我删除了~子图之间的 并%在第一个子图后添加了 。原因是两个子图都是0.5\linewidth,它们之间没有更多空间。

\documentclass[twocolumn, 10pt]{extarticle}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{csquotes}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage[hypcap = true]{subcaption}
\usepackage{tikz}
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\usetikzlibrary{shapes.geometric, positioning, automata, arrows, calc}
\begin{figure*}[!hbtp]
  \tikzstyle{square}=[rectangle,thick,minimum size=0.5cm,draw=black!100,fill=blue!20]
  \begin{subfigure}[t]{.5\linewidth}
    \centering
    \resizebox{\linewidth}{!}{
      \begin{tikzpicture}[auto, outer sep=3pt, node distance=2cm,>=latex']
        \node [square, text width = .8\textwidth] (Draw) {LalaLal aLalaLal aLalaL alaLalaLalaLal aLalaLala LalaLala};
        \node [square, below = 15mm of Draw, text width = .7\textwidth] (Matrix) {LalaLala LalaL alaLalaLalaLa laLa laLalaL alaLa laLal aLalaLala LalaL alaLal aLala};
        \node [square, below = 12mm of Matrix] (TrialMatrix) {Experiment matrix of a subset of the trial points.};
        \node [square, below = 15mm of TrialMatrix] (TrialPoint) {La laL al aLala Lala LalaL alaLa laLalaLalaLal aLalaLala};
        \node [square, below = 10mm of TrialPoint] (Pnew) {LalaLalaL ala LalaL alaLal aLala LalaLa laL alaL ala Lala };
        \node [below = 20mm of Pnew] (LogicGate) {};
        \node [square, right = 30mm of LogicGate] (Penalty) {LalaLalaLa laLal aLalaLa laLal aLa laLala};
        \node [square, below = 10mm of Penalty,fill=red!20] (PenaltyEnd) {LalaLalaLala};
        \node [square, left = 15mm of LogicGate,text width = .6\textwidth] (Replace) {RLa laLalaLal aLala LalaLa laLal aLal aLala};
        \node [square, below = 10mm of LogicGate,text width = .6\textwidth] (Criteria) {Lala LalaLala LalaLa la};
        \draw [thick,->] (Draw.south) to node [above left] {Lal aLa laLa la Lal aLa la Lala } node [above right] {fLala La laLa laLala} node [left] {Lal aL ala LalaLal a} node [right] { Lala Lala Lala Lala} node [below left] {} node [below right] {} (Matrix.north);
        \draw [thick,->] (Matrix.south) to node [left] {La laL ala LalaL al aLa la} node [right] {LalaL alaLa la Lala} node [below left] {Lala Lal aL alaLa la} node [below right] {Lal aLala Lala} (TrialMatrix.north);
        \draw [thick,->] (TrialMatrix.south) to node [above left] {Lala LalaLa laLal aLala} node[above right] {LalaL alaLal aLalaLal aLalaL alaL ala La laLal a} node [left] {LalaL alaLal aLalaLal aLalaLal aLalaL alaLalaLa laLalaLa laLala} node [right] {La laL alaLalaL alaLalaL alaLalaLal aLalaL alaLala } node [below left] {La laLalaLa laLalaLa laLalaL alaLal aLala} node [below right] {random trial point. $P_{new}=2{\times}\overline{P}-P_{r}$}(TrialPoint.north);
        \draw [thick, ->] (TrialPoint.south) -- node [left] {La la LalaLa l aLal aLa laLal aLala} node [right] {La laLal aLal aLala Lala Lala Lala} (Pnew.north);
        \draw [thick,->] (Pnew.south) -- node [above left] {LalaL alaLal aLala LalaL alaLala} node [above right] {La laLala LalaLala} node [left] {LalaL a laL alaL ala} node [right] { Lala Lal aLala } node [below left] {Lal aLa laLala} node [below right] {La laL alaLala.} (LogicGate);
        \draw [thick,->] (LogicGate) -- node [below] {LalaLala} (Penalty);
        \draw [thick,->] (LogicGate) -- node [below] {} (Criteria);
        \draw [thick,->] (LogicGate) -- node [below] {Lala Lala Lala} (Replace);
        \draw [thick,->] (Penalty) -- node[left] {Lala} node [right] {LalaLalaLala} (PenaltyEnd);
        \draw [thick,->] (Penalty.east) -- ($(Penalty.east) - (-.3,0)$) |- node [below left] {La Lalalala} (Matrix.east);
        \draw [thick,->] (Criteria.west) -- node [above] {Lala} ($(Criteria.west) - (4.6,0)$) |- (Matrix.west);
        \draw [thick,->] (Criteria.east) -- node [above] {Lqlq} (PenaltyEnd.west);
      \end{tikzpicture}
    }
    \caption{Lala
    }\label{LalaLala}
  \end{subfigure}%
  \begin{subfigure}[t]{.5\linewidth}
    \centering
    \resizebox{\linewidth}{!}{
      \begin{tikzpicture}[auto, outer sep=3pt, node distance=2cm,>=latex']
        \node [square](CRS12) {Lala LalaLalaLala Lala};
        \node [square, right = 30mm of CRS12] (CRS22) {LalaL alaLalaLalaLa laLalaLalaL laLala};
        \node [square, below = 10mm of CRS12] (LSQ12) {Lal aLalaLal aLalaL alaLal aLala LalaLala.};
        \node [square, below = 10mm of CRS22] (LSQ22) {LLa aLalaLal aLa laLal aLal aLalaala};
        \node [square] (CRSBoth) at ($(LSQ12)+(LSQ22)$) {Lal LalaLal aLalaL ala LalaLala LalaLala};
        \node[square,below of=CRSBoth] {Lala LalaL alaLalaLala LalaLalaLala Lala};
      \end{tikzpicture}
    }
    \caption{Lala.}\label{Lala}
  \end{subfigure}
  \caption{LalaLala
  }\label{LalaLalaLala}
\end{figure*}

\end{document}

在此处输入图片描述

答案2

缩放图像一方面很方便,但另一方面,图像大幅缩小时,其中的文本会很快变得难以阅读。你的情况就是这样。所以我建议你考虑重新设计你的流程图。在这方面,建议问自己以下问题:

  • 真的需要那么长的边标签吗?
  • 节点中真的需要那么长的文本吗?
  • 节点可以有多条线吗?
  • 字体大小最小是多少,不用放大镜就能看清文字?

看看下面的解决方案,其中我针对最后两个问题选择更多节点中的行数以及\footnotesize可接受的最小字体大小。除此之外,我还简化了代码,并尽可能使其简洁:

\documentclass[twocolumn, 10pt]{extarticle}
  \usepackage[utf8]{inputenc}
  \usepackage[english]{babel}
  \usepackage{csquotes}
  \usepackage[T1]{fontenc}
  \usepackage{amsmath}
  \usepackage[hypcap = true]{subcaption}
  \usepackage{tikz}
  \usetikzlibrary{arrows, positioning, }

\usepackage{showframe}% for show page layout, in real document had to be removed
\renewcommand*\ShowFrameColor{\color{red}}

\begin{document}
    \begin{figure*}
\tikzset{
       font = \footnotesize,
line/.style = {thick, -latex', shorten >=1mm, shorten <=1mm},
 box/.style = {thick, draw, fill=blue!20,
               minimum height=7mm, text width = #1, align=center},
 ELL/.style = {left, align=right},% edge label left
 ELR/.style = {right, align=left},% edge label right
 box/.default = 0.4\linewidth
        }
\begin{subfigure}[t]{.60\textwidth}
    \centering
%\resizebox{\linewidth}{!}{
    \begin{tikzpicture}[node distance = 11mm and 18mm]
    \begin{scope}[every node/.style = {box}]
\node (Draw) {LalaLal aLalaLal aLalaL alaLalaLalaLal aLalaLala LalaLala};
\node [below=of Draw] (Matrix) {LalaLala LalaL alaLalaLalaLa laLa laLalaL alaLa laLal aLalaLala LalaL alaLal aLala};
\node [below=of Matrix] (TrialMatrix) {Experiment matrix of a subset of the trial points.};
\node [below = 21mm of TrialMatrix] (TrialPoint) {La laL al aLala Lala LalaL alaLa laLalaLalaLal aLalaLala};
\node [below= of TrialPoint] (Pnew) {LalaLalaL ala LalaL alaLal aLala LalaLa laL alaL ala Lala};
\coordinate[below = 21mm of Pnew] (LogicGate) {};
    \begin{scope}[box/.default = 0.25\linewidth]
\node [right = of LogicGate] (Penalty) {LalaLalaLa laLal aLalaLa laLal aLa laLala};
\node [left =of LogicGate] (Replace) {RLa laLalaLal aLala LalaLa laLal aLal aLala};
\node [below = of LogicGate] (Criteria) {Lala LalaLala LalaLa la};
\node [at={(Criteria-| Penalty)},fill=red!20] (PenaltyEnd) {LalaLalaLala};
    \end{scope}
    \end{scope}
\draw [line]    (Draw) to  
    node [ELL]  {Lal aLa laLa la Lal aLa la Lala\\
                 Lal aL ala LalaLal a} 
    node [ELR]  {fLala La laLa laLala\\
                 Lala Lala Lala Lala} 
                (Matrix);
\draw [line]    (Matrix) to 
    node [ELL]  {La laL ala LalaL al aLa la\\
                 Lala Lal aL alaLa la} 
    node [ELR]  {LalaL alaLa la Lala\\
                  Lal aLala Lala} 
                (TrialMatrix.north);
\draw [line]    (TrialMatrix) to 
    node [ELL]  {Lala LalaLa laLal aLala LalaL \\
                 alaLal aLalaLal aLalaLal aLal \\
                 LalaLa laLalaLa laLala La laL \\
                 laLalaLa laLalaL alaLal aLala} 
    node [ELR]  {LalaL alaLal LalaLal aaL alaL\\ 
                 Lalala La laLal a La laL alaLalaL\\
                 alaLalaLal aLalaL alaLala\\
                 random trial point\\ $P_{new}=2{\times}\overline{P}-P_{r}$}
                 (TrialPoint);
\draw [line]    (TrialPoint) to 
    node [ELL]  {La la LalaLa l aLal\\
                 aLa laLal aLala} 
    node [ELR]  {La laLal aLal aLala\\
                 Lala Lala Lala} 
                (Pnew);
\draw [line]    (Pnew) to
    node [ELL] {LalaL Lal aLala LalaL alaLala\\
                LalaL a laL alaL ala\\
                Lal aLa laLala}
    node [ELR] {La laLala LalaLala\\
                Lala Lal aLala\\
                La laL alaLala.} 
                (LogicGate |- Penalty.north) -- (LogicGate);
\draw [line]    (LogicGate) to node [below] {LalaLala} (Penalty);
\draw [line]    (LogicGate) to 
    node [below,align=center] {Lala Lala\\ Lala} (Replace);
    \draw [line] (LogicGate) to (Criteria);
\draw [line]    (Penalty) to 
    node [ELL]  {Lala} 
    node [ELR]  {LalaLalaLala} 
                (PenaltyEnd);
\draw [line]    (Penalty.east) -- ++(0.4,0) |- 
    node [pos=0.75,below] {La Lalalala} 
                    (Matrix);
\draw [line]    (Criteria) -| 
    node [pos=0.25,above] {Lala} 
            ([xshift=-3mm] Replace.west) |- (Matrix.west);
\draw [line] (Criteria) to node [above] {Lqlq} (PenaltyEnd);
        \end{tikzpicture}
%        }
\caption{Lala}
    \label{LalaLala}
    \end{subfigure}
\hfill
    \begin{subfigure}[t]{.39\textwidth}
    \centering
%\resizebox{\linewidth}{!}{
    \begin{tikzpicture}[
node distance = 12mm and 4mm,
 box/.default = 0.42\linewidth,
every node/.style = {box}
                        ]
\node (CRS12) {Lala LalaLalaLala Lala};
\node [below = of CRS12] (LSQ12) {Lal aLalaLal aLalaL alaLal aLala LalaLala.};
\node [right = of CRS12] (CRS22) {LaL alaLalaLalaLa laLalaLalaL laLala};
\node [below = of CRS22] (LSQ22) {LLa aLalaLal aLa laLal aLal aLalaala};
\node [below = of LSQ22] (CRSBoth) {Lal LalaLal aLalaL ala LalaLala LalaLala};
\node [box,below = of CRSBoth]          {Lala LalaL alaLalaLala LalaLalaLala Lala};
        \end{tikzpicture}
%    }
  \caption{Lala.}\label{Lala}
    \end{subfigure}
  \caption{LalaLala}
  \label{LalaLalaLala}
  \end{figure*}
  \end{document}

在此处输入图片描述

笔记: 在提出的 MWE 中,使用了 TikZ 包的非常基本的可能性。它们在TikZ & PGF 3.0.1a 版手册在“第一部分教程和指南”和“第三部分 TikZ 没有绘画程序”中。值得一读。

关于\tikzset{...}:其中收集了所有使用的图像元素的样式,从字体大小到使用节点的功能(它被用来代替弃用tikzstyle)。使用了三种类型的节点:

  • box已定义粗边框(绘制)、蓝色填充、最小高度和文本宽度作为参数,文本以 为中心align=center。最后两个选项处理节点中的文本格式。其最大宽度受 限制text width。如果较长,则会自动换行。我根据 对齐align=center。选项text with并使align=...节点的文本行为类似于 中的文本行为\parbox{<width>}{...}
  • ELL(左侧边缘线)与您的代码新图片元素相比,后者专用于边缘左侧并与右侧对齐。 可以align= ...编写多行文本,并手动换行,如\draw (<coordinate 1>) to node[ELL] {text in the first line\\ text in the second line`` ...} (<coordinate 2>);。选择手动换行是因为可用空间取决于边缘的位置。
  • ELR (Edge Line on the Right side) are similar asELL` 但用于相对边的一侧。

除此之外,还定义了以下样式:

  • line确定边样式。这里添加了边的缩短(而不是您的选择outer sep=3pt,这可能会导致节点定位出现问题)
  • box/.default用于确定box节点中文本宽度的默认值。选择这种方式是因为第一个流程图中有两种文本宽度大小。

在每个节点tikzpicture处确定“节点距离=和,通过它们确定整幅图的流程图中所有节点之间的距离(有一个例外)。

关于使用的坐标:

  • (<coordinate 1> |- <coordinate 2>)确定通过的垂直线coordinate 1和通过的水平线的交点上的坐标coordinate 2
  • 类似地(<coordinate 1> -| <coordinate 2>)确定水平线coordinate 1和垂直线交点的坐标coordinate 2
  • 对于绘制边缘,仅使用坐标名称就足够了(无需在节点形状上明确定义锚点)。如果使用选项确定新坐标,则必须明确定义形状的锚点shift(例如,([xshift=-3mm] Replace.west)确定节点形状西侧左侧 3 毫米位置的坐标),否则会考虑锚点center

在第一张图片中,节点被分组,scope定义了共同的节点特征:它们具有box默认文本宽度的样式,即内部范围更改为较小的尺寸。通过这种方式,节点代码变得更加简洁和清晰。

有了这些措施,\adjustbox就没有必要了。我坚信预防胜于治疗,因为治疗会产生副作用(在您的情况下,图像内容无法读取,因此其价值值得怀疑)。我也希望这个答案可以作为您学习 TikZ 过程的一步 :)。

相关内容