How to draw a block on others with Tikz? - latex

Here is the diagram I am trying to make using tikz. Blocks A, B and C are inside a block (here, block with red background) and blocks E and F are inside another block. The two huge blocks (with red background) have labels on top (LabA and LabB). Moreover, I would like block E to be right and in the middle of the blocks A and B. And arrows going to block E should have some bending angle; so, not like in the one I have tried.
Here is my code.
\documentclass[varwidth,border=7]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\tikzstyle{block} = [draw=black, thick, text width=2cm, minimum height=1cm, align=center]
\tikzstyle{arrow} = [thick,->,>=stealth]
\begin{document}
\begin{tikzpicture}
\node[block] (a) {a};
\node[block, below=of a, yshift=0.7cm] (b) {b};
\node[block, below=of b, yshift=0.7cm] (c) {c};
\node[block, right=of c, xshift=0.7cm] (d) {d};
\node[block, right=of a, xshift=0.7cm, yshift=-0.4cm] (e) {e};
\draw [arrow] (a) -- (e);
\draw [arrow] (b) -- (e);
\draw [arrow] (c) -- (d);
\end{tikzpicture}
\end{document}
How could I add a rectangle over some nodes and write a label on top of it.
How to have the arrows here with a certain angle of inclination (not straight), and also to have the E block in the middle and in right of the A and B blocks. I though I could get the arrows with \draw [arrow] (a) -|- (e);, but it does not work.
Thank you in advance

I gave it a try in the hope of getting the requirements right:
\documentclass[varwidth,border=7]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\tikzstyle{block} = [draw=none, thick, text width=.4cm, minimum height=.5cm, align=center, fill=blue!50]
\tikzstyle{arrow} = [thick,->,>=stealth]
\begin{document}
\begin{tikzpicture}
\Large
%red rectangles
\node (r1) [draw=none, fill=red, minimum width=1.2cm,minimum height=3.5cm]{};
\node (r2) [right=2cm of r1.center, anchor=center, draw=none, fill=red, minimum width=1.2cm,minimum height=3.5cm]{};
\node (r1Label)[above=0cm of r1] {\textbf{LabA}};
\node (r2Label)[above=0cm of r2]{\textbf{LabB}};
%nodes
\node[block, below=3mm of r1.north, anchor=north] (a) {\color{white}A};
\node[block, below=4mm of a] (b) {\color{white}B};
\node[block, below=4mm of b] (c) {\color{white}C};
\node[block, right=2cm of c.center, anchor=center] (d) {\color{white}D};
\node at ($(a.south)+(2,-.2)$) [block] (e) {\color{white}E};
%arrows
\draw [arrow, rounded corners=2] (a) -- ($(a)+(1,0)$) |- ($(e.west)+(0,.1)$);
\draw [arrow, rounded corners=2] (b) -- ($(b)+(1,0)$) |- ($(e.west)+(0,-.1)$);
\draw [arrow] (c) -- (d);
\end{tikzpicture}
\end{document}
With this result:
Some explanations:
You can just draw rectangles in the back by defining them before the other nodes. There are more sophisticated ways using layers, but I think in this example it suffices.
I mostly used absolute distances like in the given example code. You may have to adjust... However, using these you can easily find out the middle between A and B (distance 4mm) to find the y-Position of E (a.south+ 2mm). Using ($(node1)+(node2)$) you can calculate node positions.
The -|- option you mentioned works in two steps. First draw -- to some anchor point (otherwise this is ambiguous), then |- to the target node.
The rounded corners option for draw takes the rounding radius as a parameter.

Related

can anyone guid me to plot the image in latex using tikz. i have shared the image file

I want to draw this image in a latex file using tikz and pgfplots. I have no idea I to do it. i just need the exact image shown in the image file in latex code
The code below produces this:
the output figure
\documentclass[dvipdfmx]{article}
\usepackage{tikz,bm}\usetikzlibrary{arrows.meta, angles, quotes}
\begin{document}
\begin{tikzpicture}[>=stealth, x=6mm, y=6mm, thick]
\draw (-5.5,0) -- (5.5,0) node[right] {$\boldsymbol{x}$};
\draw (0,-5.5) -- (0,5.5) node[above] {$\boldsymbol{y}$};
\draw (5,0.1) -- (5,-0.1) node[below] {$\boldsymbol{5}$};
\draw (0.1,5) -- (-0.1,5) node[left] {$\boldsymbol{5}$};
\draw (-5,0.1) -- (-5,-0.1) node[below] {$\boldsymbol{5}$};
\draw (0.1,-5) -- (-0.1,-5) node[left] {$\boldsymbol{5}$};
\draw[-{Stealth[length=3mm]}] (0,0) -- (4,-3);
\node[below] at (2,-1.5) {$\mathbf{A}$};
\draw[dotted] (4,0) -- (4,-3);
\draw[dotted] (0,-3) -- (4,-3);
\node[right] at (4,-2) {$\boldsymbol{-3}$};
\node[below] at (2,-3) {$\boldsymbol{4}$};
\coordinate (a) at (0,0); \coordinate (b) at (1,0); \coordinate (c) at (2,-1.5);
\draw pic[draw, angle radius=0.8cm, "$\boldsymbol{\alpha}$" shift={(0.2,-0.1)}] {angle=c--a--b};
\end{tikzpicture}
\end{document}

CircuiTikZ and FET drawing

Is there some simple way to avoid shorting the pins of a FET?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{circuitikz}
\begin{document}
\begin{circuitikz}
\draw (0,0)
to[I,I=$I_s$] (0,2) % The current source
to[short] (2,2);
\draw (2,0)
to node[nigfete]{FET} (2,2) % The FET transistor
to[short] (2,0) to[short] (0,0);
\draw (2,2)
to[short] (4,2)
to[R=$R$] (4,0)
to[short] (2,0);
\draw (4,2);
\draw node[rground]{};
\end{circuitikz}
\end{document}
I am not sure to understand what you want to achieve, but
use short only if you need to add poles or labels, otherwise -- is easier to type;
use the anchors of the components.
tikz is automatically loaded by circuitikz, and that one has a mandatory argument (the voltage direction standard, look at the manual and at the wanings! ;-).
So a first change could be this:
\documentclass[border=4pt] {standalone}
\usepackage[RPvoltages]{circuitikz}
\begin{document}
\begin{circuitikz}
\draw (0,0) node[rground]{}
to[I,I=$I_s$] (0,2) -- (2,2)
node[nigfete, anchor=D](F){FET} % The FET transistor
(F.S) to[short, -*] (2,0) -- (0,0);
\draw (2,2) -- (4,2)
to[R=$R$] (4,0) -- (2,0);
\draw node[rground]{};
\end{circuitikz}
\end{document}
which leads to:
then, in my opinion, it would be better to use relative positioning to make the circuit relocatable.
If you need/want the FET centered in the branch it could be better to start with it or use the calc TikZ library (already loaded by circuitikz). In the following circuit all the movement are relative, so you can move it by just changing the first (0,0):
\documentclass[border=4pt] {standalone}
\usepackage[RPvoltages]{circuitikz}
\begin{document}
\begin{circuitikz}
\draw (0,0) node[rground](GND){} to[I,I=$I_s$] ++(0,2)
-- ++(2,0) coordinate(top)
-- ++(2,0) to[R=$R$] ++(0,-2)
-- (GND-|top) coordinate(bottom)
-- (GND)
($(top)!0.5!(bottom)$) node[nigfete](F){FET}
(F.D) -- (top) (F.S) -- (bottom);
\end{circuitikz}
\end{document}

How to draw nodes with "stubs" in Latex?

I'm trying to draw this in latex.
Anyone know how?
Use the calc-library and do the following:
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node[draw=black, circle, fill=green] (A) at (0,0) {A};
\draw let \p1=(A) in (A) -- (\x1+40,\y1+10);
\end{tikzpicture}
\end{document}
Using the let-statement it allows you to specify a reference point (or even more) to use it's coordinates for further calculation.

Including floral pattern into triangles for making great dodecahedron

I am trying to make 3d figures (great dodecahedron, small stellated dodecahedron) for which till now I was using tikz package in latex. Using \coordinate and \draw functions I managed to generate exact stencil for these models.
Now instead of making polyhedrons, I am trying to add floral patterns to it using tikz only, as I am not good at sketching.
That is importing jpg images (with floral patterns) into triangles drawn using tikz so as to make stencil for polyhedrons, like:
I know how to import graphics in latex and how to make make geometric patterns using tikz, but do not know how to "crop" jpg file into triangle or any other polygon whose coordinates are specified in tikz.
For example following is the code I am using to make one surface containing 10 triangles.
\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,ifthen}
\usetikzlibrary{calc,intersections,patterns}
\usepackage[paper=a4paper,dvips,top=.7cm,left=1.5cm,right=1.5cm, foot=1.2cm,bottom=1cm]{geometry}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[scale=0.95,thin,rotate=0]
\coordinate (O) at (0cm,0cm);
\coordinate (A) at ($(O) + (36:3cm)$);
\coordinate (B) at ($(A) + (-36:3cm)$);
\coordinate (C) at ($(O) + (-36:3cm)$);
\coordinate (X) at ($(O) + (4.78mm,1.56mm)$);
\coordinate (Y) at ($(A) + (0mm,-1.9mm)$);
\coordinate (Z) at ($(B) + (-4.78mm,1.56mm)$);
\coordinate (X1) at ($(O) + (4.78mm,-1.56mm)$);
\coordinate (Y1) at ($(O) + (-36:3cm) + (0mm,1.9mm)$);
\coordinate (Z1) at ($(B) + (-4.78mm,-1.56mm)$);
\foreach \i in {0,72,...,360}{
\draw[gray, thin] let \p0=(O), \p1=(A), \p2=(B), \p3=(C) in
[rotate around={\i:(O)}] (\p2)--(\p1)--(\p0)--(\p2)--(\p3);
\draw[gray,thin,dashed] let \p1=(X), \p2=(Y), \p3=(Z) in
[rotate around={\i:(O)}] (\p1)--(\p2)--(\p3)--cycle;
\draw[gray,thin,dashed] let \p1=(X1), \p2=(Y1), \p3=(Z1) in
[rotate around={\i:(O)}] (\p1)--(\p2)--(\p3)--cycle;
}
\foreach \density in {20,30,...,160}{
\foreach \i in {0,72,...,360}{
\draw[fill=Magenta!\density] let \p1=(X), \p2=(Y), \p3=(Z) in
[rotate around={\i:(O)}] (\p1)--(\p2)--(\p3)--cycle;
}
\path
(X) coordinate (X1)
-- (Y) coordinate[pos=.15](X)
-- (Z) coordinate[pos=.15](Y)
-- (X1) coordinate[pos=.15](Z);
}
\end{tikzpicture}
\end{document}
This is the image it generates:
The problem is that the pattern in the image is also generated by tex, what I want is given any floral/non-floral pattern (that I choose) can I crop inside a triangle (the triangle is at some specific location in tex figure).
Any help would be greatly appreciated.
Thanks.
Reading pages 1062-1072 of the PGF Manual, I wrote the following test code:
\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
\filldraw [red] (0,0) rectangle (4,2);
\pgftext {\pgfimage[interpolate=true,width=1cm,height=1cm]{sample}}
\pgftext[at=\pgfpoint{28mm}{0.8cm},left,base] {\pgfimage[interpolate=true,width=1cm,height=1cm]{sample}}
\end{tikzpicture}
\end{document}
In the same folder where I saved a .tex file with the code above, I also saved this picture as sample.png. Here is a screenshot of the output of the tex compiled:
It shows the effect of positioning the png graphic using the commands \pgftext and \pgfpoint with or without attributes 28mm and 0.8cm.
Please add your feedback in a comment so we can watch deeper into it. Also, to have your code added to the question would help being more precise!
Addendum
Here is how I applied the idea in my code above to the code you added to the question:
\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,ifthen}
\usetikzlibrary{calc,intersections,patterns}
\usepackage[paper=a4paper,dvips,top=.7cm,left=1.5cm,right=1.5cm, foot=1.2cm,bottom=1cm]{geometry}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[scale=0.95,thin,rotate=0]
\coordinate (O) at (0cm,0cm);
\coordinate (A) at ($(O) + (36:3cm)$);
\coordinate (B) at ($(A) + (-36:3cm)$);
\coordinate (C) at ($(O) + (-36:3cm)$);
\coordinate (X) at ($(O) + (4.78mm,1.56mm)$);
\coordinate (Y) at ($(A) + (0mm,-1.9mm)$);
\coordinate (Z) at ($(B) + (-4.78mm,1.56mm)$);
\coordinate (X1) at ($(O) + (4.78mm,-1.56mm)$);
\coordinate (Y1) at ($(O) + (-36:3cm) + (0mm,1.9mm)$);
\coordinate (Z1) at ($(B) + (-4.78mm,-1.56mm)$);
\foreach \i in {0,72,...,360}{
\draw[gray, thin] let \p0=(O), \p1=(A), \p2=(B), \p3=(C) in
[rotate around={\i:(O)}] (\p2)--(\p1)--(\p0)--(\p2)--(\p3);
\draw[gray,thin,dashed] let \p1=(X), \p2=(Y), \p3=(Z) in
[rotate around={\i:(O)}] (\p1)--(\p2)--(\p3)--cycle;
\draw[gray,thin,dashed] let \p1=(X1), \p2=(Y1), \p3=(Z1) in
[rotate around={\i:(O)}] (\p1)--(\p2)--(\p3)--cycle;
}
\foreach \density in {20,30,...,160}{
\path
(X) coordinate (X1)
-- (Y) coordinate[pos=.15](X)
-- (Z) coordinate[pos=.15](Y)
-- (X1) coordinate[pos=.15](Z);
}
% including graphics from external file:
\foreach \i in {0,72,...,360}{
\pgftext[at=\pgfpoint{2cm*cos(\i)}{2cm*sin(\i)},left,base,rotate=\i]
{\pgfimage[interpolate=true,width=1cm,height=1cm]{sample}}
}
\end{tikzpicture}
\end{document}
To include the graphics from external file, I chose a \foreach loop, but only for the sake of using five times the same image. The key point is that the line:
\pgftext[at=\pgfpoint{2cm*cos(\i)}{2cm*sin(\i)},left,base,rotate=\i]
{\pgfimage[interpolate=true,width=1cm,height=1cm]{sample}}
should allow you to position the external graphics using the attributes 2cm*cos(\i), 2cm*sin(\i), left, base and rotate=\i only.
You may avoid the \foreach loop and use one single
\pgftext[at=\pgfpoint ... ] {\pgfimage[ ... ]{filename}}
for each of your external files. If you can generate transparent-background images for your triangle graphics, you may then enjoy this solution.
The output of mine:

How can I apply a style to existing tikz node on specific slides

This is what I'm trying to do
\begin{tikzpicture}
[node distance = 1cm, auto,font=\footnotesize,
% STYLES
every node/.style={node distance=1.3cm},
comment/.style={rectangle, inner sep= 5pt, text width=4cm, node distance=0.25cm, font=},
module/.style={rectangle, drop shadow, draw, fill=black!10, inner sep=5pt, text width=3cm, text badly centered, minimum height=0.8cm, font=\bfseries\footnotesize\sffamily,rounded corners},
selected/.style={fill=red!40}]
\node [module] (nodeA) {node A};
\node [module, below of=nodeA] (nodeA) {node B};
\only<1>{
\node [comment, text width=6cm, right=0.25 of nodeA] {short description of Node A};
\node [comment, text width=6cm, right=0.25 of nodeB] {short description of Node B};
}
\only<2>{
\node [selected] (nodeA) {};
\node [comment, text width=6cm, right=0.25 of nodeA] {long description of node A};
}
\only<3>{
\node [selected] (nodeB) {};
\node [comment, text width=6cm, right=0.25 of nodeA] {long description of node B};
}
\end{tikzpicture}
The problem is
\node [selected] (nodeB) {};
creates a new node, but I want it to apply the style for the existing node. Is there any way to do so?
Of course I could have copies of every node in selected state and not-selected state, but I really want to have a normal solution.
I don't think you can do this the way you want to (assuming I understand the question correctly), because once a node is drawn, there's no way to change its appearance. I'd suggest using Beamer's \alt macro:
\alt<2>{\node[module,selected] at (nodeA) {node A};}{\node[module] at (nodeA) {node A};}
\alt<3>{\node[module,selected] at (nodeB) {node B};}{\node[module] at (nodeB) {node B};}
\node[comment,text width=6cm,right=0.25 of nodeA]{\alt<2>{short description}{long description}};
\node[comment,text width=6cm,right=0.25 of nodeB]{\alt<3>{short description}{long description}};
Or something like that (you might have to tinker with the semicolons to get it to work, I can't test that at the moment).
Another option would be to actually just draw a new node. If you include
\node[module,selected] at (nodeA) {node A};
inside \only<2>, that will draw a node that looks just like node A, except with a red background, at the same position at node A. The new node will cover up the original node A.
Sometimes, to avoid repetitions, it may be nice to do something like this:
% #1 Overlay specs.
% #2 Style name.
% #4 Style properties.
\def\onlystyle<#1>#2#3{%
\alt<#1>{%
\tikzset{#2/.style = {#3}}
}{%
\tikzset{#2/.style = {}}
}%
}
Then, if you put, for example, this within a frame:
\onlystyle<2>{selected}{fill = red}
the style selected will be defined as fill = red on the second slide of the animation, and as a style with no effect whatsoever on every other slide. Then, you can write a readable figure such as:
\begin{tikzpicture}
\node at (0, 0) {A};
\node[selected] at (1, 0) {B};
\node at (2, 0) {C};
\end{tikzpicture}
and the “B” node will be highlighted on the second slide. This way, you don't have to copy-paste tons of node definitions.
Of course, it cannot be applied to every single animation need, but I like to keep this technique up my sleeve.
I found yet another solution, which has advantages (more functionality!) compared to all previous solutions, including the one I posted before.
First I mention the improved solution, then I explain why it actually shows more functionality than all other solutions.
The following solution, adapted from How can I make Beamer overlays with TikZ node attributes?, uses an additional tikz library and uses a parameter for the slide number-dependent attribute (for the slide numbers, of course). Note that the tikz setting has to be done outside the frame now.
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{overlay-beamer-styles}
\begin{document}
\begin{frame}{With code/node duplication (explicit frame numbers)}
\begin{tikzpicture}
[every node/.style={draw,circle},
redStyle/.style={fill=red},
blueStyle/.style={fill=blue}]
\node<1> [] (A) {A}; % no style
\node<2> [redStyle] (A) {A}; % red style
\node<3> [blueStyle] (A) {A}; % blue style
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\tikzset{
every node/.style={draw,circle},
redStyle/.style={fill=red}, redStyle on/.style={alt=#1{redStyle}{}},
blueStyle/.style={fill=blue}, blueStyle on/.style={alt=#1{blueStyle}{}}}
\begin{frame}{\textbf{Without} code/node duplication (explicit frame numbers)}
\begin{tikzpicture}
\node [redStyle on=<2>,blueStyle on=<3>] (A) {A};
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\tikzset{
every node/.style={draw,circle},
redStyle/.style={fill=red}, redStyle on/.style={alt=#1{redStyle}{}},
blueStyle/.style={fill=blue}, blueStyle on/.style={alt=#1{blueStyle}{}}}
\begin{frame}{\textbf{Without} code/node duplication (relative frame numbers)}
\begin{tikzpicture}
\node [redStyle on=<+(1)>,blueStyle on=<+(1)>] (A) {A};
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\end{document}
Now the explanation why this solution is the best mentioned so far, i.e. which additional features it has and why (or when) they are relevant. Well, simply because the respective property does not have to show at the same slide number for different usages within the same frame. Simple example:
Let's say you have a simple tree, i.e. nodes with directed edges connecting them. Say you want to make some of the edges bold in specific slides. Of course you have multiple edges, so clearly they do not all become bold at the very same time! Some edges get bold at frame numbers m to n, others get bold at x and y. This can now be trivially realized with using the node (or edge, in this case) properties timedBold on=<m-n> and timedBold on=<x,y>.
Note that there is yet another possibility, which to me seems a bit better than the previous two suggestions, because it has less code duplication (compared to David Z's solution), and because (compared to Alice M.'s solution) you do not need to define a new command that you had to define outside the frame (though defining this additional command is probably not an issue to anybody). In principle, the following suggestion seems to be closely related to the suggestion by Alice M.
Anyway, the solution is a direct application of How to modify a node in TikZ when using beamer's overlays.
Is basically just re-defines a style based on the frame number. See the following minimal example, second or third frame. (I re-did the minimal example since I regard the given one extremely complicated; it was certainly not a minimal example, and also not working on its own.)
\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}{With code/node duplication (explicit frame numbers)}
\begin{tikzpicture}
[every node/.style={draw,circle},
redStyle/.style={fill=red},
blueStyle/.style={fill=blue}]
\node<1> [] (A) {A}; % no style
\node<2> [redStyle] (A) {A}; % red style
\node<3> [blueStyle] (A) {A}; % blue style
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\begin{frame}{\textbf{Without} code/node duplication (explicit frame numbers)}
\only<1>{\tikzset{colorStyle/.style={}}} % no style
\only<2>{\tikzset{colorStyle/.style={fill=red}}} % red style
\only<3>{\tikzset{colorStyle/.style={fill=blue}}} % blue style
\begin{tikzpicture}
[every node/.style={draw,circle}]
\node [colorStyle] (A) {A}; % frame-dependent style
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\begin{frame}{\textbf{Without} code/node duplication (relative frame numbers)}
\only<+>{\tikzset{colorStyle/.style={}}} % no style
\only<+>{\tikzset{colorStyle/.style={fill=red}}} % red style
\only<+>{\tikzset{colorStyle/.style={fill=blue}}} % blue style
\begin{tikzpicture}
[every node/.style={draw,circle}]
\node [colorStyle] (A) {A}; % frame-dependent style
\node [right of=A] (B) {B};
\draw [-latex] (A) -- (B) ;
\end{tikzpicture}
\end{frame}
\end{document}

Resources