1

I want to draw a Flowchart in latex the main idea is in this image

enter image description here

and I try to do it as this

enter image description here

by use this code

\documentclass[12pt]{report}

\usepackage{tikz} \usetikzlibrary{shapes.geometric, arrows,positioning}

\tikzstyle{startstop} = [rectangle, text width=3cm, minimum height=1cm,text centered, draw=black, fill=red!30] \tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, text width=3cm, minimum height=1cm, text centered, draw=black, fill=blue!30] \tikzstyle{process} = [rectangle, text width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30] \tikzstyle{decision} = [diamond, text width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30] \tikzstyle{arrow} = [thick,->,>=stealth]

\begin{document}

\begin{tikzpicture}[node distance=0.8cm,font=\sf]

\node (in1) [startstop] {\textbf{Input}: Original sample $X$}; \node (pro1) [process, below=of in1] {pre-test for normality}; \node (dec1) [decision, below=of pro1,xshift=5cm] {$H_0^{(1)}$ is not rejected (Normal)}; \node (dec2) [decision, below=of pro1,xshift=-5cm] {$H_0^{(1)}$ is rejected (Not normal)}; \node (pro2) [process, below=of dec1] {Apply t-test}; \node (pro3) [process, below=of dec2] {Apply Wilcoxon test}; \node (dec3) [decision, below=of pro2,xshift=2.25cm] {$H_0^{(2t)}$ is not rejected}; \node (dec4) [decision, below=of pro2,xshift=-2.25cm] {$H_0^{(2t)}$ is rejected }; \node (dec5) [decision, below=of pro3,xshift=2.25cm] {$H_0^{(2W)}$ is not rejected}; \node (dec6) [decision, below=of pro3,xshift=-2.25cm] {$H_0^{(2W)}$ is rejected }; \node (in2) [startstop, right=of in1]{ Bootstrap sample };

\draw [arrow] (in1) -- (pro1); \draw [arrow] (pro1) -- (dec1); \draw [arrow] (pro1) -- (dec2); \draw [arrow] (dec1) -- (pro2); \draw [arrow] (dec2) -- (pro3); \draw [arrow] (pro2) -- (dec3); \draw [arrow] (pro2) -- (dec4); \draw [arrow] (pro3) -- (dec5); \draw [arrow] (pro3) -- (dec6); \draw [arrow,dashed] (in1)-- node[anchor=south] {Draw} (in2) ; \draw [arrow,dashed] (in2) |- (pro1);

\end{tikzpicture}

\end{document}

Is this way correct and how can I improve this code?

  • 1
    Welcome to SE. Please provide a Minimal Working Example (MWE), so that people can 1) copy-paste your code and compile it directly, 2) see which packages you're using. Thanks. – Miyase May 18 '22 at 10:19
  • There is a tutorial you might be interested in: https://latexdraw.com/draw-flowcharts-latex-tutorial/ – DG' May 18 '22 at 10:29

1 Answers1

1

I was able to run the code as you supplied it, but the graphic didn't fit on the page. It seems that without scaling it in some way, it's just too wide. You might consider using the changepage package with its adjustwidth macro to narrow the margins temporarily and the center environment to center the graphic within the newly widened margins. I did not double-check to see if the flowchart produced by the code accurately reflects the flowchart you provided by hand, but I'm sure you can do that.

Here is the code as I modified it:

\documentclass[12pt]{report} 
\usepackage{changepage}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows,positioning}

\tikzstyle{startstop} = [rectangle, text width=3cm, minimum height=1cm,text centered, draw=black, fill=red!30] \tikzstyle{io} = [trapezium, trapezium left angle=70, trapezium right angle=110, text width=3cm, minimum height=1cm, text centered, draw=black, fill=blue!30] \tikzstyle{process} = [rectangle, text width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30] \tikzstyle{decision} = [diamond, text width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30] \tikzstyle{arrow} = [thick,->,>=stealth] \begin{document} \begin{adjustwidth}{-2.5cm}{-2.5cm} \begin{center} \begin{tikzpicture}[node distance=0.8cm,font=\sf] \node (in1) [startstop] {\textbf{Input}: Original sample $X$}; \node (pro1) [process, below=of in1] {pre-test for normality}; \node (dec1) [decision, below=of pro1,xshift=5cm] {$H_0^{(1)}$ is not rejected (Normal)}; \node (dec2) [decision, below=of pro1,xshift=-5cm] {$H_0^{(1)}$ is rejected (Not normal)}; \node (pro2) [process, below=of dec1] {Apply t-test}; \node (pro3) [process, below=of dec2] {Apply Wilcoxon test}; \node (dec3) [decision, below=of pro2,xshift=2.25cm] {$H_0^{(2t)}$ is not rejected}; \node (dec4) [decision, below=of pro2,xshift=-2.25cm] {$H_0^{(2t)}$ is rejected }; \node (dec5) [decision, below=of pro3,xshift=2.25cm] {$H_0^{(2W)}$ is not rejected}; \node (dec6) [decision, below=of pro3,xshift=-2.25cm] {$H_0^{(2W)}$ is rejected }; \node (in2) [startstop, right=of in1]{ Bootstrap sample };

  \draw [arrow] (in1) -- (pro1);
  \draw [arrow] (pro1) -- (dec1);
  \draw [arrow] (pro1) -- (dec2);
  \draw [arrow] (dec1) -- (pro2);
  \draw [arrow] (dec2) -- (pro3);
  \draw [arrow] (pro2) -- (dec3);
  \draw [arrow] (pro2) -- (dec4);
  \draw [arrow] (pro3) -- (dec5);
  \draw [arrow] (pro3) -- (dec6);
  \draw [arrow,dashed]   (in1)-- node[anchor=south] {Draw} (in2) ;
  \draw [arrow,dashed] (in2) |- (pro1);
\end{tikzpicture}

\end{center} \end{adjustwidth} \end{document}

The resultant output is below. The label Draw at the top is too wide for the arrow as shown, so moving the Bootstrap sample node further away would help.

enter image description here

Another approach would be to use the standalone document class in place of the report class. You could then use \includegraphics from the graphicx package to include the resultant graphics file and scale the graphic into whatever margins you think are appropriate.