3

I am following this example (Block Diagrams in TikZ) on how to put circuitikz elements inside a node box using \newcommand. The elements seems to be centered when outside the \newcommand, but when I tried using them inside a Tikz picture, the alignment was lost. How to I center align, both vertically and horizontally, the \newcommand elements (circuitikz) inside the rectangular node box. In the example I as following, the figures are neatly aligned inside the nodes. What am I missing? Thanks for the help.

Below is what I have so far.

% For drawing block diagrams, plotting, circuits, etc
\documentclass{beamer}
\usepackage{tikz}
\usepackage{circuitikz}
\usetikzlibrary{shapes, arrows, arrows.meta, positioning, calc, quotes, backgrounds,     intersections, fit, matrix}
\usepackage{amsmath}

\begin{document}

\begin{frame}{Tikz picture inside a node}

\newcommand{\Sampler}{ \begin{circuitikz} \draw (0,0) to [opening switch] (2.5,0); \node (label) at (1.25, -0.4){$F_s=1//T$}; \end{circuitikz} }

% This is supposed to be inside the box \begin{circuitikz} \draw (0,0) to [opening switch] (2.5,0); \node (label) at (1.25, -0.4){$F_s=1//T$}; \end{circuitikz} \qquad % But when I use the \newcommand \Sampler, alignment is lost \begin{tikzpicture}[auto, node distance=2cm,>=latex'] \coordinate (IN) at (0,0); \node [draw, fill=blue!20, rectangle, minimum height=1.5cm, minimum width=1.5cm, right = 2.0cm of IN, name=SAMP1]{\Sampler}; \coordinate[right = 2cm of SAMP1] (OUT);

\draw[->] (IN)node[left, align=left]{Analog\\signal} to node[midway]{$\scriptstyle x_a(t)$}(SAMP1);
\draw[->] (SAMP1) to node[midway]{$\scriptstyle x(n)\equiv x_a(nT)$} (OUT)node[right, align=left]{Discrete-time\\signal};

\end{tikzpicture} \end{frame}

\end{document}

Rhandley
  • 391

1 Answers1

2

Remedy for your problem is consider @Rmano comment below (original answer). Problem arise by transfer of settings outer tikzpicture to inner one.

original answer: This you can avoid in your particular case by use text height instead minimum height(first example) or removeminimum height` from outer node options (second example).

First example:

\documentclass{beamer}
\usepackage{circuitikz}
\usetikzlibrary{arrows.meta, 
                %backgrounds, calc, fit, intersections, matrix,
                positioning, 
                quotes,      
                }

\begin{document} \begin{frame}[fragile] \frametitle{Tikz picture inside a node}

\newcommand{\Sampler}{% \begin{circuitikz} \draw (0,0) to [opening switch, a={$F_s=1/T$}] (2.5,0); \end{circuitikz} }

\Sampler

\begin{center} \begin{tikzpicture}[auto, node distance = 2cm, every edge/.style = {draw, -Latex}, every label/.style = {align=left}, every edge quotes/.style = {font=\scriptsize} ] \coordinate[label=left:Analog\signal] (IN); \node (SAMP1) [draw, text height=15mm, fill=blue!20, right=of IN] {\Sampler}; \coordinate[label=right:Discrete-time\signal, right=of SAMP1] (OUT);

\draw (IN) edge ["$x_a(t)$"] (SAMP1) (SAMP1) edge ["$x(n)\equiv x_a(nT)$"] (OUT); \end{tikzpicture} \end{center} \end{frame} \end{document}

enter image description here

Second example:

enter image description here

Addedndum: Considering proper nesting of TikZ pictures the MWE is:

\usepackage{circuitikz}
\usetikzlibrary{arrows.meta, 
                positioning, 
                quotes,      
                }
\newbox\Sampler
\sbox{\Sampler}{%
\begin{circuitikz}
    \draw (0,0) to [opening switch, a={$f_s=1/T$}] (2.5,0);
\end{circuitikz}}

\begin{document} \begin{frame}[fragile] \frametitle{Tikz picture inside a node}

\usebox{\Sampler}

\begin{center}
\begin{tikzpicture}[auto, 

node distance = 22mm, every edge/.style = {draw, -Latex}, every label/.style = {align=left}, every edge quotes/.style = {font=\footnotesize}, N/.style = {draw, fill=blue!20, minimum size=16mm, inner sep=1ex} ] \coordinate[label=left:Analog\signal] (IN); \node (SAMP1) [N, right=of IN] {\usebox{\Sampler}}; \coordinate[label=right:Discrete-time\signal, right=of SAMP1] (OUT);

\draw (IN) edge ["$x_a(t)$"] (SAMP1) (SAMP1) edge ["$x(n)\equiv x_a(nT)$"] (OUT); \end{tikzpicture} \end{center} \end{frame} \end{document}

which gives the similar result as second example:

enter image description here

Zarko
  • 296,517
  • In simple cases like this it works, but notice that your are nesting tikzpictures, and it will fail sooner or later. Better use a box: https://tex.stackexchange.com/a/66037/38080 – Rmano Feb 20 '21 at 09:00
  • 1
    @Rmano, you are right. I add addendum to answer where is considered link which you kindly provide me. – Zarko Feb 20 '21 at 09:41
  • Thanks for the help. Using \mybox solves the problem. – Rhandley Feb 20 '21 at 10:28