将 tikzpicture 与文本宽度对齐

将 tikzpicture 与文本宽度对齐

我有这个 tikz 图形,但遗憾的是它与文本宽度不对齐。我该如何实现?

\documentclass[12pt]{article}
\usepackage[onehalfspacing]{setspace}
\usepackage[left=3cm,right=3.5cm,top=2.5cm,bottom=2.75cm]{geometry} 
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{calc}
\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm, text centered, draw=black, text width=3cm]
\tikzstyle{startstopnoborder} = [minimum width=3cm, minimum height=1cm, text centered, text width=3cm]
\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, text width=3cm]
\tikzstyle{arrow} = [thick,->,>=stealth]
\usepackage{amssymb}
\usetikzlibrary{shapes.geometric, arrows, positioning}
\begin{document}
\begin{center}
\begin{tikzpicture}[node distance=1.5cm]
    \centering
    \node (add) [startstop] {Add new columns to the restricted master problem};
    \node (solve1) [startstop, below=of add] {Solve subproblem for physician $i=1$};
    \node (solve2) [startstop, below=of solve1] {Solve subproblem for physician $i=2$};
    \node (dot) [startstopnoborder, below=of solve2] {$\vdots$};
    \node (solven) [startstop, below=of dot] {Solve subproblem for physician $i=\mid I\mid$};
    \node (solvethe) [startstop, left=of solve2,] {Solve the restricted LP master problem and obtain dual cost};
    \node (construct) [startstop, left=of solvethe] {Construct initial restricted master problem with a basic solution / heuristic};
    \node (reduce) [decision, right=of solve2] {Reduce cost $<$ 0};
    \node (lp) [startstop, right=of reduce] {LP relaxation is optimal and find IP solution};
    \node (empty1) [coordinate, right=1cm of solvethe] {};
    \node (empty2) [coordinate, left=1cm of reduce] {};
    
    \draw [arrow] (construct) -- (solvethe);
    \draw [arrow] (add) -| (solvethe);
    \draw [arrow] (reduce) |- node[fill=white,] {Yes} (add);
    \draw [arrow] (reduce) -- node[fill=white,] {No} (lp);
    
    \draw (solvethe) -- (empty1);
    \draw [arrow] (empty1) |- (solve1);
    \draw [arrow] (empty1) |- (solve2);
    \draw [arrow] (empty1) |- (solven);
    
    \draw [arrow] (empty2) -- (reduce);
    \draw (solve1) -| (empty2);
    \draw (solve2) -| (empty2);
    \draw (solven) -| (empty2);
    
\end{tikzpicture}
\end{center}
\end{document}  

答案1

通用方法是将图表排版为独立图片(使用文档类standalone),并将图片包含为 \includegraphics[width=\textwidth]{chart.pdf}。这将相应地缩放图片。

或者,您可以将流程图设计得更小,包括使用更小的字体,并注意可用空间,见下文。 在此处输入图片描述

\documentclass[12pt]{article}
\usepackage[left=3cm,right=3.5cm,top=2.5cm,bottom=2.75cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, positioning}
\tikzset{
  startstop/.style = {draw, rounded corners, text width=2cm},
  decision/.style = {diamond, draw, text width=1.5cm},
  arrow/.style = {thick,rounded corners,-stealth}
}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{center}
\begin{tikzpicture}[node distance=0.8cm]\footnotesize
    \centering
    \node (add) [startstop] {Add new columns to the restricted master problem};
    \node (solve1) [startstop, below=of add] {Solve subproblem for physician $i=1$};
    \node (solve2) [startstop, below=of solve1] {Solve subproblem for physician $i=2$};
    \node (solven) [startstop, below=of solve2] {Solve subproblem for physician $i=|I|$};
    \node (solvethe) [startstop, left=of solve2,] {Solve the restricted LP master problem and obtain dual cost};
    \node (construct) [startstop, left=of solvethe] {Construct initial restricted master problem with a basic solution / heuristic};
    \node (reduce) [decision, right=of solve2] {Reduce cost $<$ 0};
    \node (lp) [startstop, right=of reduce] {LP relaxation is optimal and find IP solution};
    
    \draw [arrow] (construct) -- (solvethe);
    \draw [arrow] (add) -| (solvethe);
    \draw [arrow] (reduce) |- node[pos=0.1,above,sloped] {Yes} (add);
    \draw [arrow] (reduce) -- node[above] {No} (lp);
    
    \draw [arrow] (solvethe) -- coordinate(empty1) (solve2);
    \draw [arrow] (solve2) -- coordinate(empty2) (reduce);
    
    \draw [arrow] (solvethe) -| (empty1) |- (solve1);
    \draw [arrow] (solve1) -| (empty2) |- (reduce);
    
    \draw [arrow] (solvethe) -- (empty1) |- (solven);
    \draw [arrow] (solven) -| (empty2) -- (reduce);
    
    \path (solve2) -- node[pos=0.4]{$\vdots$} (solven);
\end{tikzpicture}
\end{center}
\lipsum[2]
\end{document}

答案2

您可以使用adjustbox

请注意\tikzstyle已被弃用多年:请参阅下面的代码以了解更好的方法,这也可以避免代码重复。 也arrows已过时,您应该加载arrows.meta

该包showframe仅用于显示图片确实缩放到文本宽度。

\documentclass[12pt]{article}

\usepackage{showframe}% just for the example

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{adjustbox}
\usepackage{tikz}
\usetikzlibrary{chains}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.geometric, arrows.meta, positioning}

\tikzset{
  startstopnoborder/.style = {
    minimum width=3cm,
    minimum height=1cm,
    text centered,
    text width=3cm
  },
  startstop/.style = {
    startstopnoborder,
    rectangle,
    rounded corners,
    draw=black,
  },
  decision/.style = {
    startstopnoborder,
    diamond,
    draw=black,
  },
  arrow/.style = {thick,->,>=stealth},
}

\begin{document}

\begin{center}
\begin{adjustbox}{max width=\textwidth}
\begin{tikzpicture}[node distance=1.5cm]
    \centering
    \node (add) [startstop] {Add new columns to the restricted master problem};
    \node (solve1) [startstop, below=of add] {Solve subproblem for physician $i=1$};
    \node (solve2) [startstop, below=of solve1] {Solve subproblem for physician $i=2$};
    \node (dot) [startstopnoborder, below=of solve2] {$\vdots$};
    \node (solven) [startstop, below=of dot] {Solve subproblem for physician $i=\mid I\mid$};
    \node (solvethe) [startstop, left=of solve2,] {Solve the restricted LP master problem and obtain dual cost};
    \node (construct) [startstop, left=of solvethe] {Construct initial restricted master problem with a basic solution / heuristic};
    \node (reduce) [decision, right=of solve2] {Reduce cost $<$ 0};
    \node (lp) [startstop, right=of reduce] {LP relaxation is optimal and find IP solution};
    \node (empty1) [coordinate, right=1cm of solvethe] {};
    \node (empty2) [coordinate, left=1cm of reduce] {};
    
    \draw [arrow] (construct) -- (solvethe);
    \draw [arrow] (add) -| (solvethe);
    \draw [arrow] (reduce) |- node[fill=white,] {Yes} (add);
    \draw [arrow] (reduce) -- node[fill=white,] {No} (lp);
    
    \draw (solvethe) -- (empty1);
    \draw [arrow] (empty1) |- (solve1);
    \draw [arrow] (empty1) |- (solve2);
    \draw [arrow] (empty1) |- (solven);
    
    \draw [arrow] (empty2) -- (reduce);
    \draw (solve1) -| (empty2);
    \draw (solve2) -| (empty2);
    \draw (solven) -| (empty2);
    
\end{tikzpicture}
\end{adjustbox}
\end{center}

\end{document}  

在此处输入图片描述

答案3

如果您不想使用外部文件,还有其他选项,首先,您可以将所有 tikz 代码缩减为单个标记,甚至可以利用可以缩放、旋转和重复使用整个图形这一事实,当然,为了避免出现不良结果,您必须声明该transform shape选项,如果不这样做,则只会修改坐标,而节点将保持相同的方向。如您所见,我做了 3 个选项,两个初始选项要求您手动查看哪种比例最适合您,这在将段落拼凑在一起时非常有用。如果在最后一张纸上您只占用一小部分,通过减小图形的尺寸,您可以使文本保留在前一张纸上,第三个选项只是强制图形具有指定的宽度。

结果:

在此处输入图片描述

梅威瑟:

\documentclass[12pt]{article}
\usepackage[onehalfspacing]{setspace}
\usepackage[left=3cm,right=3.5cm,top=2.5cm,bottom=2.75cm]{geometry} 
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{lipsum}
\usetikzlibrary{chains}
\usetikzlibrary{calc}
\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm, text centered, draw=black, text width=3cm]
\tikzstyle{startstopnoborder} = [minimum width=3cm, minimum height=1cm, text centered, text width=3cm]
\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, text width=3cm]
\tikzstyle{arrow} = [thick,->,>=stealth]
\usepackage{amssymb}
\usetikzlibrary{shapes.geometric, arrows, positioning}

\def\myPic#1#2{
    \begin{tikzpicture}[node distance=1.5cm,scale=#1,rotate=#2,transform shape]
        \centering
        
        \node (add) [startstop] {Add new columns to the restricted master problem};
        \node (solve1) [startstop, below=of add] {Solve subproblem for physician $i=1$};
        \node (solve2) [startstop, below=of solve1] {Solve subproblem for physician $i=2$};
        \node (dot) [startstopnoborder, below=of solve2] {$\vdots$};
        \node (solven) [startstop, below=of dot] {Solve subproblem for physician $i=\mid I\mid$};
        \node (solvethe) [startstop, left=of solve2,] {Solve the restricted LP master problem and obtain dual cost};
        \node (construct) [startstop, left=of solvethe] {Construct initial restricted master problem with a basic solution / heuristic};
        \node (reduce) [decision, right=of solve2] {Reduce cost $<$ 0};
        \node (lp) [startstop, right=of reduce] {LP relaxation is optimal and find IP solution};
        \node (empty1) [coordinate, right=1cm of solvethe] {};
        \node (empty2) [coordinate, left=1cm of reduce] {};
        
        \draw [arrow] (construct) -- (solvethe);
        \draw [arrow] (add) -| (solvethe);
        \draw [arrow] (reduce) |- node[fill=white,] {Yes} (add);
        \draw [arrow] (reduce) -- node[fill=white,] {No} (lp);
        
        \draw (solvethe) -- (empty1);
        \draw [arrow] (empty1) |- (solve1);
        \draw [arrow] (empty1) |- (solve2);
        \draw [arrow] (empty1) |- (solven);
        
        \draw [arrow] (empty2) -- (reduce);
        \draw (solve1) -| (empty2);
        \draw (solve2) -| (empty2);
        \draw (solven) -| (empty2);         
    \end{tikzpicture}
}

\begin{document}
    \lipsum[1]
    
    \begin{figure}[h!]
        \centering
        \myPic{0.5}{0}
        \caption{Some text}
    \end{figure}

    \lipsum[2]  
    \begin{figure}[h!]
        \centering
        \myPic{0.74}{90}
        \caption{Some text}
    \end{figure}
    
    \lipsum[3-4]
    
    \begin{figure}
        \centering
        \resizebox{\textwidth}{!}{\myPic{1}{0}}
        \caption{Some text}
    \end{figure}

\end{document}  

相关内容