2

I am trying to place an anchor next to decision node but with the current code I have, it places it at the end of the arrow.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{comment}
\usetikzlibrary{shapes.geometric,arrows}

\tikzstyle{startstop}=[rectangle,rounded corners, minimum width=3cm,minimum height=1cm, text centered, draw=black, fill = red!30] \tikzstyle{io}=[trapezium,trapezium left angle = 70,trapezium right angle = 110, minimum width=4cm ,minimum height=1cm, text centered, trapezium stretches=true, draw=black, fill = blue!30] \tikzstyle{process}=[rectangle,minimum width=3cm,minimum height=1cm, text centered,text width = 4cm, draw=black, fill = orange!30] \tikzstyle{decision} = [diamond,minimum width=3cm,minimum height=1cm, text centered, draw=black, fill = green!30] \tikzstyle{arrow} = [thick,->,>=stealth] \begin{document} \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(mesh)[process,below of=start]{Form the Mesh}; \node(initialize_pot)[process,below of=mesh]{Initialize \ potential using charge neutrality}; \node(initial_poisson_coeff)[process,below of=initialize_pot,yshift=-0.7cm]{Calculate the poisson coefficients and the forcing function using the initialized potential}; \node(solve_poisson_firsttime)[process,below of=initial_poisson_coeff,yshift=-0.5cm]{Solve the \ poisson equation}; \node(first_potential)[io,below of=solve_poisson_firsttime,yshift=0.1cm]{Potential Profiles}; \node(schrodinger_solver)[process,right of=start,xshift=8cm]{Solve the schr"{o}dinger equation using potential profile}; \node(wavefunctions)[io,below of=schrodinger_solver]{Wavefunctions and Energies}; \node(electron_density)[process,below of=wavefunctions] {Calculate \ electron density in the schrodinger domain}; \node(update_poisson_coefficients)[process,below of = electron_density,yshift=-1.7cm]{Use wavefunctions and eigen energies to update the central coefficients and forcing function of the grid points in the schrodinger domain. For the other points, use potential from previous iteration}; \node(solve_poisson)[process,below of=update_poisson_coefficients,yshift=-1.5cm]{solve poisson equation}; \node(converged_check)[decision, below of=solve_poisson, yshift=-0.5cm] {Converged?}; \node(stop)[startstop,below of=converged_check,yshift=-0.5cm]{Stop};

\drawarrow--(mesh); \drawarrow--(initialize_pot); \drawarrow--(initial_poisson_coeff); \drawarrow-- (solve_poisson_firsttime); \drawarrow--(first_potential); \draw[arrow] (first_potential) - ++(5,0) -- ++(5,11) -- (schrodinger_solver); \drawarrow--(wavefunctions); \drawarrow--(electron_density); \drawarrow -- (update_poisson_coefficients); \drawarrow -- (solve_poisson); \drawarrow -- (converged_check); \drawarrow- ++(4,0) -- ++(4,13.75) --node[anchor=north] {no} (schrodinger_solver); \drawarrow --node[anchor = east] {yes} (stop);

\begin{comment} \drawarrow|-(proc1); \drawarrow--(stop); \end{comment} \end{tikzpicture} \end{document}

How can I accomplish this?

  • Hi, welcome. I don't really understand what you're asking, can you try explaining it more in detail? (Small tip by the way: try \draw[arrow] (first_potential) - ++(5,0) |- (schrodinger_solver); instead of what you have now.) – Torbjørn T. Sep 06 '20 at 08:30
  • Thanks for the tip. Coming to my question, you can see in the above code that an arrow goes from the decision node converged_check to the node schrodinger_solver. Also, I am anchoring the text no. However, with the current code I have, the text no is appearing at the end of the arrow. I want the text to appear at the beginning of the arrow. Let me know if this is unclear. – prananna Sep 06 '20 at 08:46
  • That was clear, thanks. (I was confused by the term anchor, when you add a node at at certain point, e.g. \node at (x,y) {foo};, the anchor of the node determines which point of the node is placed on that point. The default anchor is the center, meaning that the center of the node is at (x,y), but if you do e.g. \node [anchor=north west] at (x,y) {foo}; the top left corner of the node is placed at (x,y).) – Torbjørn T. Sep 06 '20 at 08:51

1 Answers1

3

I think you want

\draw[arrow](converged_check) -- node[above] {no} ++(4,0) |- schrodinger_solver);

which will place that node midway along the first segment of the path, i.e. the horizontal part leaving the converged-node.

If you want it midway along the vertical section of the path, use

\draw[arrow](converged_check) -- ++(4,0) |- node[pos=0.25,left]  {no}   (schrodinger_solver);

The pos key is for positioning nodes at relative positions along a path. Using a perpendicular path specification as I did here (i.e. (a) |- (b), see TikZ: What EXACTLY does the the |- notation for arrows do?), is a special case though, then pos=0.5 always corresponds to the corner of the path, so pos=0.25 is halfway along the first segment.

As a side note, you're using some syntax that is outdated, notably the right of= etc. and \tikzstyle. For the former it's recommended to load the positioning library and use right=of instead, but note that the behaviour is a little bit different. See Difference between "right of=" and "right=of" in PGF/TikZ For the latter it's recommended to use \tikzset{foo/.style={...}, bar/.style={...}}. (Can perhaps make an example later this evening, don't have time right now.)

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{comment}
\usetikzlibrary{shapes.geometric,arrows}

\tikzstyle{startstop}=[rectangle,rounded corners, minimum width=3cm,minimum height=1cm, text centered, draw=black, fill = red!30] \tikzstyle{io}=[trapezium,trapezium left angle = 70,trapezium right angle = 110, minimum width=4cm ,minimum height=1cm, text centered, trapezium stretches=true, draw=black, fill = blue!30] \tikzstyle{process}=[rectangle,minimum width=3cm,minimum height=1cm, text centered,text width = 4cm, draw=black, fill = orange!30] \tikzstyle{decision} = [diamond,minimum width=3cm,minimum height=1cm, text centered, draw=black, fill = green!30] \tikzstyle{arrow} = [thick,->,>=stealth] \begin{document} \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(mesh)[process,below of=start]{Form the Mesh}; \node(initialize_pot)[process,below of=mesh]{Initialize \ potential using charge neutrality}; \node(initial_poisson_coeff)[process,below of=initialize_pot,yshift=-0.7cm]{Calculate the poisson coefficients and the forcing function using the initialized potential}; \node(solve_poisson_firsttime)[process,below of=initial_poisson_coeff,yshift=-0.5cm]{Solve the \ poisson equation}; \node(first_potential)[io,below of=solve_poisson_firsttime,yshift=0.1cm]{Potential Profiles}; \node(schrodinger_solver)[process,right of=start,xshift=6cm]{Solve the schr"{o}dinger equation using potential profile}; \node(wavefunctions)[io,below of=schrodinger_solver]{Wavefunctions and Energies}; \node(electron_density)[process,below of=wavefunctions] {Calculate \ electron density in the schrodinger domain}; \node(update_poisson_coefficients)[process,below of = electron_density,yshift=-1.7cm]{Use wavefunctions and eigen energies to update the central coefficients and forcing function of the grid points in the schrodinger domain. For the other points, use potential from previous iteration}; \node(solve_poisson)[process,below of=update_poisson_coefficients,yshift=-1.5cm]{solve poisson equation}; \node(converged_check)[decision, below of=solve_poisson, yshift=-0.5cm] {Converged?}; \node(stop)[startstop,below of=converged_check,yshift=-0.5cm]{Stop};

\drawarrow--(mesh); \drawarrow--(initialize_pot); \drawarrow--(initial_poisson_coeff); \drawarrow-- (solve_poisson_firsttime); \drawarrow--(first_potential); \draw[arrow] (first_potential) -- ++(4,0) |- (schrodinger_solver); \drawarrow--(wavefunctions); \drawarrow--(electron_density); \drawarrow -- (update_poisson_coefficients); \drawarrow -- (solve_poisson); \drawarrow -- (converged_check); \drawarrow -- node[above] {no} ++(4,0) |- (schrodinger_solver); % or this: %\drawarrow -- ++(4,0) |- node[pos=0.25,left] {no} (schrodinger_solver); \drawarrow --node[anchor = east] {yes} (stop);

\begin{comment} \drawarrow|-(proc1); \drawarrow--(stop); \end{comment} \end{tikzpicture} \end{document}

Edit:

As mentioned a more modernized version of the code follows. Note there is no need to adjust the node spacing using yshift or similar when using below=of .., because the space is calculated between the edges of the nodes, rather than the center points.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{
  shapes.geometric,
  arrows.meta, % not actually used, but supersedes the old arrows library
  positioning,
  }

\tikzset{ startstop/.style={rectangle, rounded corners, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill = red!30}, io/.style={trapezium, trapezium left angle = 70, trapezium right angle = 110, minimum width=4cm ,minimum height=1cm, text centered, trapezium stretches=true, draw=black, fill = blue!30}, process/.style={rectangle,minimum width=3cm,minimum height=1cm, text centered,text width = 4cm, draw=black, fill = orange!30}, decision/.style={diamond,minimum width=3cm,minimum height=1cm, text centered, draw=black, fill = green!30}, arrow/.style={thick,->,>=stealth} } \begin{document} \begin{tikzpicture}[node distance=1cm]

\node (start) [startstop] {Start}; \node (mesh) [process,below=of start]{Form the Mesh}; \node (initialize_pot) [process,below=of mesh] {Initialize \ potential using charge neutrality}; \node (initial_poisson_coeff) [process,below=of initialize_pot] {Calculate the poisson coefficients and the forcing function using the initialized potential}; \node (solve_poisson_firsttime) [process,below=of initial_poisson_coeff] {Solve the \ poisson equation}; \node (first_potential) [io,below=of solve_poisson_firsttime] {Potential Profiles}; \node (schrodinger_solver) [process,right=3cm of start] {Solve the schr"{o}dinger equation using potential profile}; \node (wavefunctions) [io,below=of schrodinger_solver] {Wavefunctions and Energies}; \node (electron_density) [process,below=of wavefunctions] {Calculate \ electron density in the schrodinger domain}; \node (update_poisson_coefficients) [process,below=of electron_density] {Use wavefunctions and eigen energies to update the central coefficients and forcing function of the grid points in the schrodinger domain. For the other points, use potential from previous iteration}; \node (solve_poisson)[process,below=of update_poisson_coefficients] {solve poisson equation}; \node (converged_check) [decision, below=of solve_poisson] {Converged?}; \node (stop) [startstop,below=of converged_check] {Stop};

\draw[arrow] (start) -- (mesh); \draw[arrow] (mesh) -- (initialize_pot); \draw[arrow] (initialize_pot) -- (initial_poisson_coeff); \draw[arrow] (initial_poisson_coeff) -- (solve_poisson_firsttime); \draw[arrow] (solve_poisson_firsttime) -- (first_potential); \draw[arrow] (first_potential) -- ++(3,0) |- (schrodinger_solver); \draw[arrow] (schrodinger_solver) -- (wavefunctions); \draw[arrow] (wavefunctions) -- (electron_density); \draw[arrow] (electron_density) -- (update_poisson_coefficients); \draw[arrow] (update_poisson_coefficients) -- (solve_poisson); \draw[arrow] (solve_poisson) -- (converged_check); \draw[arrow] (converged_check) -- node[above] {no} ++(3,0) |- (schrodinger_solver); % or this: %\drawarrow -- ++(4,0) |- node[pos=0.25,left] {no} (schrodinger_solver); \drawarrow --node[anchor = east] {yes} (stop);

\end{tikzpicture} \end{document}

Torbjørn T.
  • 206,688