LaTex/TikZ - Box alignment - alignment

I created following LaTeX/TikZ script to draw a figure:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{arrows, chains}
\usetikzlibrary{positioning,shapes.multipart}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[draw, fill=yellow!96!black, rectangle,
align=center, inner sep=3ex, font=\sffamily, label=below: Label1] (Box1) {
\begin{tikzpicture}
\node[draw, shape=rectangle split, rectangle split parts=2,
inner sep=1ex, rounded corners=0pt,
fill=white, font=\vphantom{Q}\sffamily] (Innerbox) {One \nodepart{two} Two};
\end{tikzpicture}};
\node[draw, inner sep=1ex, right=of Box1, label=below: Label2] (Box2) {
\begin{tikzpicture}
\end{tikzpicture}
{\begin{tikzpicture}
\node[draw, fill=yellow!96!black, rectangle,
align=center, inner sep=3ex, font=\sffamily, label=below: Label3] (Box3) {Four};
\end{tikzpicture}}};
\draw (Box1) -- (Box2) -- (Box3);
\draw[dashed] (Innerbox.one east) -- (Box2);
\draw[dashed] (Innerbox.two east) -- (Box2);
\end{tikzpicture}
\end{document}
I want to solve following problems:
How can the boxes be aligned equal, so than the label below is always on the same level?
How can box "Four" be added (not within, but as a box like Box1) to the right of Box2, ideally with a dashed line from Box2 to Box3?
Desired structure:
[Box1] -- [Box2] -- [Box3]

My elementary approach to this picture:
\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
% left box
\filldraw [fill=yellow, draw=black, ultra thick] (0,0) rectangle (8,-10);
\filldraw [fill=white, draw=black, ultra thick] (2,-2) rectangle (6,-5);
\filldraw [fill=white, draw=black, ultra thick] (2,-5) rectangle (6,-8);
\node at (4,-3.5) [font=\sffamily] {\Huge One};
\node at (4,-6.5) [font=\sffamily] {\Huge Two};
% right box
\filldraw [fill=white, draw=black, ultra thick] (12,0) rectangle (20,-10);
\filldraw [fill=yellow, draw=black, ultra thick] (13,-1) rectangle (19,-6);
\node at (16,-3.5) [font=\sffamily] {\Huge Four};
% labels
\node at (4,-11) {\Huge 1};
\node at (16,-11) {\Huge 2};
\node at (16,-7) {\Huge 3};
% lines
\draw [ultra thick] (8,-5) -- (12,-5);
\draw [loosely dashed, ultra thick] (6,-3.5) -- (12,-4);
\draw [loosely dashed, ultra thick] (6,-6.5) -- (12,-6);
\end{tikzpicture}
\end{document}
And its output:
Here you can play with coordinates to align boxes as you like.
This tries to answer to part 1 of your question while part 2 of it is not well clear to me. Anyway if you like this code, you can develop it and edit box Four just by using \filldraw, \draw and \node as above.

Related

Draw a grid with 32 cells, intermediate rectangles does not show up

I would like to draw a figure describing the floating-point IEEE754 specification.
Doing so, I tried to draw a grid with 32 rectangles. (I then figured out there is a grid command).
Here is a MWE for 4 cells:
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\newcommand{\width}{1}
\newcommand{\height}{2}
\foreach \x in {0, 1}{
\draw (\x, 0) rectangle (\x+\width, \height);
}
\foreach \x in {2, 3}{
\draw (\x, 0) rectangle (\x+\width, \height);
}
\end{tikzpicture}
\end{document}
I used two foreach. I could merge these into one, but I then obtain only the rectangles on the far left and far right side, not the four rectangles as in the first MWE. Why is it so ?
\begin{tikzpicture}
\newcommand{\width}{1}
\newcommand{\height}{2}
\foreach \x in {0, 4}{
\draw (\x, 0) rectangle (\x+\width, \height);
}
\end{tikzpicture}
By using \foreach \x in {0, 4} your list literally just has these two values, no intermediate values.
If you like to include intermediate steps, you can use \foreach \x in {0,...,3}
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\newcommand{\width}{1}
\newcommand{\height}{2}
\foreach \x in {0,...,3}{
\draw (\x, 0) rectangle (\x+\width, \height);
}
\end{tikzpicture}
\end{document}

How to draw following figure or similar using tikz and pgf

My thesis superviser asks to redraw this picture in order to cite it in my paper. Could someone help how? Maybe there is a software that can make drawn picture a latex code
enter image description here
Perhaps this will get you started. First make a suitable layout of the boxes by editing the foreach loop. After that, make the boxes one-by-one, e.g.
\draw (2,7) node[rectangle,fill=green!20]{$C$};
\draw (3,7) node[rectangle,fill=green!20]{$T_1$};
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes.symbols}
\begin{document}
\begin{tikzpicture}[block/.style={signal, draw, signal to=north}]
\filldraw[fill=gray!20,rounded corners=5mm] (0,0) rectangle (10,10);
\filldraw[fill=blue!30!white,very thick] (1,4) rectangle (9,8);
\foreach \x in {2,3,4,5,6,7,8} {
\draw (\x,3) node[block,fill=yellow!50]{text};
\draw (\x,5) node[rectangle,fill=red!20]{text};
\draw (\x,7) node[rectangle,fill=green!20]{text};
\draw[->,line width=1mm] (\x,3.5) -- (\x,4.5);
}
\begin{scope}[shift={(13,0)}]
% #1, code needed also below
\filldraw[fill=gray!20,rounded corners=5mm] (0,0) rectangle (10,10);
\end{scope}
\begin{scope}[shift={(15,-0.2)}]
\filldraw[fill=gray!20,rounded corners=5mm] (0,0) rectangle (10,10);
\end{scope}
\begin{scope}[shift={(17,-0.4)}]
\filldraw[fill=gray!20,rounded corners=5mm] (0,0) rectangle (10,10);
\draw (0,0) circle (2pt);
\filldraw[fill=gray!20,rounded corners=5mm] (0,0) rectangle (10,10);
% copy paste contents from #1 and edit a little
\filldraw[fill=blue!30!white,very thick] (1,4) rectangle (9,8);
\foreach \x in {2,3,4,5,6,7,8} {
\draw (\x,3) node[block,fill=yellow!50]{text};
\draw (\x,5) node[rectangle,fill=red!20]{text};
\draw (\x,7) node[rectangle,fill=green!20]{text};
\draw[->,line width=1mm] (\x,3.5) -- (\x,4.5);
}
\end{scope}
\end{tikzpicture}
\end{document}

trying to connect two boxes in flowchart latex

I am using overleaf and trying to connect like following.
see image here
The code is shared using overleaf
https://www.overleaf.com/3275194655nwtxkzncnhqt
Quick and dirty hack for the arrow: \draw[arrow] ([xshift=-0.5cm]Modify AC coefficients.south) -| ([xshift=3cm]Watermarked Image.north);
Please
note that you should not use \tikzstyle, this is obsolete.
also check that latin1 is really the appropriate encoding for your document. The vast majority of editors use utf8 by default.
don't misuse x for \times
\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\begin{document}
\pagestyle{empty}
\tikzset
{
arrow/.style={
->,>=stealth}
}
% Define block styles
\tikzstyle{decision} = [diamond, draw,
text width=4.5em, text badly centered, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{block1} = [rectangle, draw,
text width=6em, text centered, rounded corners, minimum height=4em]
\tikzstyle{block2} = [rectangle, draw,
text width=15em, text centered, rounded corners, minimum height=4em]
\tikzstyle{block3} = [rectangle, draw,
text width=20em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\begin{tikzpicture}[node distance = 2.5cm, auto]
% Place nodes
\node [block3] (Watermarked Image) {Watermarked Image};
\node[decision, above of =Watermarked Image,node distance=4cm ] (Difference between selected coefficients) {Difference between selected coefficients };
\node [block, left of=Difference between selected coefficients, node distance=3.5cm] (Inverse DCT for each block)
{Inverse DCT for each block};
\node [block, right of=Difference between selected coefficients, node distance=3.5cm] (Modify AC coefficients)
{Modify AC coefficients and perform Inverse DCT for each block};
\node [block2, above of=Difference between selected coefficients] (Genetic Algorithm)
{Genetic Algorithm search DCT coefficients to find best option for insert watermark};
\node [block, above of=Genetic Algorithm] (DCT for each block)
{DCT for each block};
\node [block, left of=DCT for each block] (Watermark Message)
{Watermark Message};
\node [block, above of=DCT for each block] (8x8 Image Blocking)
{8x8 Image Blocking};
\node [block, above of=Watermark Message] (Host Image)
{Host Image};
% Draw edges
\path [line] (Host Image) -- (8x8 Image Blocking);
\path [line] (8x8 Image Blocking) -- (DCT for each block);
\path [line] (DCT for each block) -- (Genetic Algorithm);
%\path [line] (Watermark Message) -- (Genetic Algorithm);
%\path [line] (Modify AC coefficients) -- (Inverse DCT for each block);
%\path [line] (Inverse DCT for each block) -- (Watermarked Image);
\path [line] (Genetic Algorithm) -- (Difference between selected coefficients);
\path [line] (Difference between selected coefficients) -- node {yes} (Inverse DCT for each block);
\path [line] (Difference between selected coefficients) -- node {no}(Modify AC coefficients);
\coordinate (n2nl) at ([xshift=-2cm]Genetic Algorithm.north);
\coordinate (n2nr) at ([xshift=-3cm]Watermarked Image.north);
\coordinate (n2ns) at ([xshift=3cm]Watermarked Image.east);
\draw[arrow] (Watermark Message.south -| n2nl) -- (n2nl);
\draw[arrow] (Inverse DCT for each block.south -| n2nr) -- (n2nr);
% \draw[arrow] (Modify AC coefficients.south -| n2ns) -- (n2ns);
\draw[arrow] ([xshift=-0.5cm]Modify AC coefficients.south) -| ([xshift=3cm]Watermarked Image.north);
\end{tikzpicture}
\end{document}

Latex tikz flowchart: how to move the label to blank spaces

I'm currently using Latex to make flowcharts and I found that the arrow goes through the label like this:
Surely with the codes:
%!TEX program = xelatex UTF8
\documentclass[UTF8]{ctexart}
\usepackage{tikz,mathpazo}
\usepackage{graphicx,amssymb,amstext,amsmath,newtxmath}
\usetikzlibrary{shapes.geometric, arrows}
\begin{document}
\thispagestyle{empty}
\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=3cm, minimum height=1cm, text centered, draw=black, fill=blue!30]
\tikzstyle{process} = [rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30]
\tikzstyle{decision} = [diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30]
\tikzstyle{arrow} = [->,>=stealth]
\begin{tikzpicture}[node distance=2cm]
\node (start) [startstop] {Start};
\node (in1) [io, below of=start] {Input 1};
\node (pro1) [process, below of=in1] {Process 1};
\node (dec1) [decision, below of=pro1, yshift=-0.5cm] {Decision 1};
\node (out1) [io, left of=dec1,xshift=-2cm,yshift=-2cm] {Out 1};
\node (stop) [startstop, below of=out1] {Stop};
\node (dec2) [decision,right of=dec1,xshift=2cm,yshift=-2cm] {Decision 2};
\node (pro2) [process, right of=dec2, xshift=2cm] {Process 2};
\node (pro3) [process, below of=dec2, yshift=-0.5cm] {Process 3};
\draw [arrow](start) -- (in1);
\draw [arrow](in1) -- (pro1);
\draw [arrow](pro1) -- (dec1);
\draw [arrow](dec1) -| node[anchor=north] {Doch} (out1);
\draw [arrow](out1) -- (stop);
\draw [arrow](dec1) |- node[anchor=east] {Nein} (dec2);
\draw [arrow](dec2) -- node[anchor=east] {Nein} (pro3);
\draw [arrow](dec2) -- node[anchor=south] {Ja} (pro2);
\draw [arrow](pro2) |- (dec1);
\end{tikzpicture}
\end{document}
How can I move the label Doch to the blank space on the sides?
Use an additional left or above attribute:
\draw [arrow](dec1) -| node[anchor=north, left] {Doch} (out1); or
\draw [arrow](dec1) -| node[anchor=north, above] {Doch} (out1); should do the trick.

Incorrect alignment of nodes in subpictures

I am using tikz to typeset a diagram in a latex document.
I have a grid of 'grid-diagrams', with each grid-diagram drawn as a separate tikz picture and then arranged into the grid as nodes.
I want to draw a circle node (which will eventually contain a label) in the bottom-right hand square of each grid-diagram.
I use exactly the same code for each grid-diagram, but the circle is drawn in a different place each time.
Am I doing something wrong, or is this a bug in tikz? In particular, is my approach of including sub-pictures allowed/standard/good practice?
See here for an image.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
\begin{document}
\begin{tikzpicture}[scale=1, node distance = .5cm]
\node (a) at (0,0) {
\begin{tikzpicture}
\draw[step=.5cm,gray,very thin] (-0.1,-1.6) grid (1.6, 0.1);
\node at (1.25, -1.25) [fill=white, shape=circle, draw=black] {};
\end{tikzpicture}
};
\node[right=of a] (b) {
\begin{tikzpicture}
\draw[step=.5cm,gray,very thin] (-0.1,-1.6) grid (1.6, 0.1);
\node at (1.25, -1.25) [fill=white, shape=circle, draw=black] {};
\end{tikzpicture}
};
\node[below=of a] (c) {
\begin{tikzpicture}
\draw[step=.5cm,gray,very thin] (-0.1,-1.6) grid (1.6, 0.1);
\node at (1.25, -1.25) [fill=white, shape=circle, draw=black] {};
\end{tikzpicture}
};
\node[right=of b] (d){
\begin{tikzpicture}
\draw[step=.5cm,gray,very thin] (-0.1,-1.6) grid (1.6, 0.1);
\node at (1.2, -1.5) [fill=white, shape=circle, draw=black] {};
\end{tikzpicture}
};
\node[below=of b] (e){
\begin{tikzpicture}
\draw[step=.5cm,gray,very thin] (-0.1,-1.6) grid (1.6, 0.1);
\node at (1.25, -1.25) [fill=white, shape=circle, draw=black] {};
\end{tikzpicture}
};
\node[below = of c] (f) {
\begin{tikzpicture}
\draw[step=.5cm,gray,very thin] (-0.1,-1.6) grid (1.6, 0.1);
\node at (1.25, -1.25) [fill=white, shape=circle, draw=black] {};
\end{tikzpicture}
};
\end{tikzpicture}
\end{document}
I am not sure why the circles are not aligned in your example, but it may have to do with the relative positioning using of.
I don't know if this is a best practice, but you can use foreach to automate the grid construction:
\begin{document}
\begin{tikzpicture}[scale=1]
\foreach \x in {-0.01cm,2.99cm,5.99cm}
\foreach \y in {-0.01cm,2.99cm,5.99cm} {
\draw[step=.5cm,gray,thin] (\x,\y) grid +(1.52cm,1.52cm);
\draw[fill=white, draw=black] (\x,\y) +(1.255cm,0.25cm) circle (0.2cm);
}
\end{tikzpicture}
\end{document}

Resources