3

I'm trying to draw simple block diagram in via:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[-,auto,node distance=1.25cm] \tikzstyle{point}=[coordinate] \tikzstyle{block}=[draw, rectangle, minimum height=1em, minimum width=1.5em] \node[point] (0) {}; \node[point] (1) [right of=0 ] {}; \node[block] (2) [above right of=1] {A}; \node[block] (3) [right of=2] {B}; \node[block] (4) [right of=3] {C}; \node[block] (7) [right of=1] {A}; \node[block] (8) [right of=7] {E}; \node[block] (9) [right of=8] {D}; \node[block] (12) [below right of=1] {B}; \node[block] (13) [right of=12] {C}; \node[block] (14) [right of=13] {E}; \node[point] (15) [right of=4] {}; \node[point] (16) [right of=9] {}; \node[point] (17) [right of=14] {}; \node[point] (18) [right of=16] {};

\draw [thick]  (12) -| (1) (7) -|  (1)   (2) -| (1) ;
\draw [thick]   (0) --  (1)   (2) --  (3) (7) --  (8)   (3) -- (4)  (8) -- (9)  (12) -- (13);
\draw [thick]   (13) -- (14) ;
\draw [thick]   (15) -- (4) ;
\draw [thick]   (16) -- (9) ;
\draw [thick]   (17) -- (14) ;
\draw [thick]   (18) -- (16) ;
\draw [thick] (15) -| (16)    (17) -| (16);

\end{tikzpicture} \end{document}

This gives:

The result

What I have trouble with is proper alignment between boxes and general symmetry of diagram. Namely I'd like to have lines on the left and on the right of the same length. Although I'm rather an occasional Latex user I'd appreciate hint as well as some alternative and maybe cleaner way to draw such a diagram.

SebGlav
  • 19,186
borg
  • 257
  • 1
    Do you want the nodes to be vertically aligned? I think a \matrix would be a better usage. By the way, the right of syntax is considered to be depecrated and is replaced by the positioning library and the right=of syntax. (Whether this is part of your problem or solution I haven't checked. You'd want to use on grid then, probably) So is \tikzstyle but that's really offtopic. – Qrrbrbirlbel Oct 26 '23 at 19:08
  • Without a matrix, you want to steer clear of diagonals, they're messy. But with positioning you could use node distance=1.25cm and 1.25cm for the diagonals. But you can avoid this by placing an A right of 1, and the other A above of that A and the B below the first A. But again, a \matrix makes this very easy and flexible. Or even TikZ-CD. – Qrrbrbirlbel Oct 26 '23 at 19:12

2 Answers2

3

Do you mean withy "symetry" something like this?

enter image description here

By help of libraries chains (for shorter code) and positioning (for simpler positioning of nodes) code for your image can be rewritten to:

\documentclass[margin=3mm]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,
                positioning}

\begin{document}

\begin{tikzpicture}[ node distance = 4mm and 4mm, start chain = going right, N/.style = {draw, thin, minimum height=1em, minimum width=1.5em}, every path/.append style = {semithick} ] \scoped[nodes={N, on chain, join=by -}] { \coordinate (0); \coordinate (1); \node (7) {A}; \node (8) {E}; \node (9) {D}; \coordinate (15); \coordinate (16); } \node (2) [N,above=of 7] {A}; \node (3) [N,above=of 8] {B}; \node (4) [N,above=of 9] {C}; % \node (12) [N,below=of 7] {B}; \node (13) [N,below=of 8] {C}; \node (14) [N,below=of 9] {E};

\draw[red, densely dashed, very thin]
(2.north west) -- (14.south east) (12.south west) -- (4.north east); \draw (1) |- (2) -- (3) -- (4) -| (15) (1) |- (12) -- (13) -- (14) -| (15); \end{tikzpicture} \end{document}

Zarko
  • 296,517
2

A solution with nicematrix and TikZ.

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\pgfset{nicematrix/cell-node/.append style = { minimum size = 6 mm } }

\begin{NiceTabular}{ccc}[pgf-node-code=\pgfusepathqstroke] A & B & C \[4mm] A & E & D \[4mm] B & C & E \CodeAfter \begin{tikzpicture} \draw (1-1) -- (1-2) (1-2) -- (1-3) ; \draw (2-1) -- (2-2) (2-2) -- (2-3) ; \draw (3-1) -- (3-2) (3-2) -- (3-3) ; \draw (1-1) -- ++(-6mm,0pt) |- (3-1) ; \draw (1-3) -- ++(6mm,0pt) |- (3-3) ; \draw (2-1) -- ++(-8mm,0pt) (2-3) -- ++(8mm,0pt) ;
\end{tikzpicture} \end{NiceTabular}

\end{document}

Output of the above code

F. Pantigny
  • 40,250
  • Nice answer (first +1 was mine)! For experiment: if to preamble you add \usetikzlibrary{ext.paths.ortho}, than you can draw lines as \begin{tikzpicture} \foreach \i in {1,2,3} \draw (\i-1) -- (\i-2) -- (\i-3); \draw (1-1) r-lr (3-1) (1-3) r-rl (3-3) ; \draw (2-1.west) -- ++(-9mm,0pt) (2-3.east) -- ++(9mm,0pt) ; \end{tikzpicture} :-) – Zarko Oct 27 '23 at 21:06