1

I'm completely new to Tikz...

I draw the following tikzpicture thanks to exemple found on the internet:

enter image description here

The code for Beamer class here:

\documentclass[aspectratio=169,compress,8pt,svgnames,dvipsnames]{beamer}
\usetheme{Warsaw}
\usecolortheme{seahorse}
\useoutertheme[footline=authortitle,subsection=true]{miniframes}

\usepackage{tikz}
\usetikzlibrary{mindmap,shapes,arrows,calc,arrows.meta}
\usepackage{verbatim}
\usepackage{bondgraphs}


% Define a few tikz styles and constants
% Theme Color
\colorlet{subsystemPartBgColor}{blue}%
\colorlet{subsystemPartFgColor}{red}%
\colorlet{subsystemPartBorderColor}{green}%
\colorlet{subsystemBgColor}{yellow}%
\colorlet{subsystemFgColor}{black}%
\colorlet{subsystemBorderColor}{black!50}%
% Block
\newcommand*{\subssytemFontStyle}[1]{\textbf{#1}}% Text Font Style For Subsystem
\tikzstyle{subsystemStyle} = [text=subsystemFgColor, font=\bfseries]%
\tikzstyle{sensor}=[draw, fill=blue!20, text width=5em,text centered, minimum height=2.5em]%
\tikzstyle{SubSysPart} = [sensor, text width=6em, fill=subsystemPartBgColor,minimum height=8em, rounded corners, text=subsystemPartFgColor, draw=subsystemPartBorderColor]% Part of a subsystem
% Arrow
% !!! Needs \usetikzlibrary{arrows.meta}
\tikzstyle{ann} = [above, text width=6em]% Arrow for Phisical Quantity wich are not Flows or Effort in Bond Graph 
\tikzstyle{signal_num} = [-latex, color=red!30!black]% For signal numeric Arrows
\tikzstyle{signal_ana} = [-{Latex[open]}]% For signal analog Arrows
\tikzstyle{env} = [-{Stealth[open]}]% For environement Arrows
\tikzstyle{Pf_out} = [bond, f_out]% For f_out Bond Arrow
\tikzstyle{Pe_out} = [bond, e_out]% For e_out Bond Arrow
\tikzstyle{Pf_in} = [bond, f_in]% For f_in Bond Arrow
\tikzstyle{Pe_in} = [bond, e_in]% For e_in Bond Arrow
\tikzstyle{var} = [->]% For Variable Bond Arrow
\tikzstyle{miss} = [-{Implies},double]% For Mission Bond Arrow

\begin{document}

\begin{frame}[allowframebreaks]
\begin{figure}[h]
% We need layers to draw the block diagram
\pgfdeclarelayer{background}%
\pgfdeclarelayer{foreground}%
\pgfsetlayers{background,main,foreground}%
%
% Define constants
\def\blockdist{3.2}%
\def\envdist{1.7}%
\def\comdist{1.2}%
\def\edgedist{2}%
\def\inletdist{2}%
%
\begin{tikzpicture}%
%# Subsystem part Node Block
%## Compressor Performance Model Block
\node (throttle) [SubSysPart] {Throttle};%
%## Compressor Inertia Block
% Note the use of \path instead of \node at ... below. 
\path (throttle.west)+(-\blockdist,0) node (manifil) [SubSysPart] {manifold Filling};%
%## Command and Environement Block (invisible block, just for arrows)
\path (manifil.west)+(-\inletdist,0) node (inlet) [minimum height=8em] {};%
%## Susystem Background Block
\path [subsystemStyle] (throttle.south west)+(-2.3,-0.5) node (CPSs) {\subssytemFontStyle{supply manifold Subsystem}};%
% Now it's time to draw the colored rectangles.
% To draw them behind the blocks we use pgf layers. This way we  
% can use the above block coordinates to place the backgrounds   
    \begin{pgfonlayer}{background}%
        % Compute a few helper coordinates
        \path (manifil.west |- throttle.north)+(-0.3,0.2) node (a) {};%
        \path (CPSs.south -| throttle.east)+(+0.3,-0.2) node (b) {};%
        \path[fill=subsystemBgColor,rounded corners, draw=subsystemBorderColor, dashed]
            (a) rectangle (b);%
        \path (manifil.north west)+(-0.2,0.2) node (a) {};%
    \end{pgfonlayer}%
%# Arrows
% Unfortunately we cant use the convenient \path (fromnode) -- (tonode) 
% syntax here. This is because TikZ draws the path from the node centers
% and clip the path at the node boundaries. We want horizontal lines, but
% the sensor and throttle blocks aren't aligned horizontally. instead we use
% the line intersection syntax |- to calculate the correct coordinate
%## Variable Arrows
\path [draw, var] ( $ (manifil.south east)!0.3!(manifil.east) $ ) -- node [above] {psmin$=$psmout} ( $ (throttle.south west)!0.3!(throttle.west) $ )  ;% 
\path [draw, var] (manifil.east) -- node [above] {Tsmin$=$Tsmout} (throttle.west)  ;% 
     \draw [var] ( $ (inlet.south east)!0.5!(inlet.east) $ )  -- node [above,near start] {Tsmin$=$Tcpout} ( $ (manifil.south west)!0.5!(manifil.west) $ ) ;%
%### Outlet
%## Power Arrows 
%### Inlet
     \draw [Pf_in] ( $ (inlet.north east)!0.5!(inlet.east) $ )  -- node [above,near start] {Qsmin$=$Qcp} ( $ (manifil.north west)!0.5!(manifil.west) $ ) ;%
     \draw [Pe_out] (inlet.east)  -- node [above,near start] {psmin$=$pcpout} (manifil.west) ;%
%### Outlet     
     \draw [Pf_out] ( $ (manifil.north east)!0.3!(manifil.east) $ )  -- node [above] {Qsmout} ( $ (throttle.north west)!0.3!(throttle.west) $ ) ;%
% We could simply have written (manifil) .. (throttle.140). However, it's
% best to avoid hard coding coordinates
\draw [Pf_out] ( $ (throttle.north east)!0.60!(throttle.east) $ ) -- node [ann, pos=0.61] {Mass Flow Rate} + (\edgedist,0) 
        node[right] {Qsmout};%
\draw [Pf_out] ( $ (throttle.south east)!0.40!(throttle.east) $ ) -- node [ann, pos=0.61] {Fuel Cell Pressure} + (\edgedist,0) 
        node[right] {psmout$=p_{fc,in}$};%
\end{tikzpicture}
\end{figure}
\end{frame}

\end{document}

I set all the "constants" to have good positioning, no overlaping, etc.

But when I tried to use this figure in an Article DocumentClass I got this (text overlapings circled in red):

enter image description here

Here the code:

\documentclass{article}
\usepackage{xcolor}

\usepackage{tikz}
\usetikzlibrary{mindmap,shapes,arrows,calc,arrows.meta}
\usepackage{verbatim}
\usepackage{bondgraphs}

% Define a few tikz styles and constants
% Theme Color
\colorlet{subsystemPartBgColor}{blue}%
\colorlet{subsystemPartFgColor}{red}%
\colorlet{subsystemPartBorderColor}{green}%
\colorlet{subsystemBgColor}{yellow}%
\colorlet{subsystemFgColor}{black}%
\colorlet{subsystemBorderColor}{black!50}%
% Block
\newcommand*{\subssytemFontStyle}[1]{\textbf{#1}}% Text Font Style For Subsystem
\tikzstyle{subsystemStyle} = [text=subsystemFgColor, font=\bfseries]%
\tikzstyle{sensor}=[draw, fill=blue!20, text width=5em,text centered, minimum height=2.5em]%
\tikzstyle{SubSysPart} = [sensor, text width=6em, fill=subsystemPartBgColor,minimum height=8em, rounded corners, text=subsystemPartFgColor, draw=subsystemPartBorderColor]% Part of a subsystem
% Arrow
% !!! Needs \usetikzlibrary{arrows.meta}
\tikzstyle{ann} = [above, text width=6em]% Arrow for Phisical Quantity wich are not Flows or Effort in Bond Graph 
\tikzstyle{signal_num} = [-latex, color=red!30!black]% For signal numeric Arrows
\tikzstyle{signal_ana} = [-{Latex[open]}]% For signal analog Arrows
\tikzstyle{env} = [-{Stealth[open]}]% For environement Arrows
\tikzstyle{Pf_out} = [bond, f_out]% For f_out Bond Arrow
\tikzstyle{Pe_out} = [bond, e_out]% For e_out Bond Arrow
\tikzstyle{Pf_in} = [bond, f_in]% For f_in Bond Arrow
\tikzstyle{Pe_in} = [bond, e_in]% For e_in Bond Arrow
\tikzstyle{var} = [->]% For Variable Bond Arrow
\tikzstyle{miss} = [-{Implies},double]% For Mission Bond Arrow

\begin{document}

%\begin{frame}[allowframebreaks]
\begin{figure}[h]
% We need layers to draw the block diagram
\pgfdeclarelayer{background}%
\pgfdeclarelayer{foreground}%
\pgfsetlayers{background,main,foreground}%
%
% Define constants
\def\blockdist{3.2}%
\def\envdist{1.7}%
\def\comdist{1.2}%
\def\edgedist{2}%
\def\inletdist{2}%
%
\begin{tikzpicture}%
%# Subsystem part Node Block
%## Compressor Performance Model Block
\node (throttle) [SubSysPart] {Throttle};%
%## Compressor Inertia Block
% Note the use of \path instead of \node at ... below. 
\path (throttle.west)+(-\blockdist,0) node (manifil) [SubSysPart] {manifold Filling};%
%## Command and Environement Block (invisible block, just for arrows)
\path (manifil.west)+(-\inletdist,0) node (inlet) [minimum height=8em] {};%
%## Susystem Background Block
\path [subsystemStyle] (throttle.south west)+(-2.3,-0.5) node (CPSs) {\subssytemFontStyle{supply manifold Subsystem}};%
% Now it's time to draw the colored rectangles.
% To draw them behind the blocks we use pgf layers. This way we  
% can use the above block coordinates to place the backgrounds   
    \begin{pgfonlayer}{background}%
        % Compute a few helper coordinates
        \path (manifil.west |- throttle.north)+(-0.3,0.2) node (a) {};%
        \path (CPSs.south -| throttle.east)+(+0.3,-0.2) node (b) {};%
        \path[fill=subsystemBgColor,rounded corners, draw=subsystemBorderColor, dashed]
            (a) rectangle (b);%
        \path (manifil.north west)+(-0.2,0.2) node (a) {};%
    \end{pgfonlayer}%
%# Arrows
% Unfortunately we cant use the convenient \path (fromnode) -- (tonode) 
% syntax here. This is because TikZ draws the path from the node centers
% and clip the path at the node boundaries. We want horizontal lines, but
% the sensor and throttle blocks aren't aligned horizontally. instead we use
% the line intersection syntax |- to calculate the correct coordinate
%## Variable Arrows
\path [draw, var] ( $ (manifil.south east)!0.3!(manifil.east) $ ) -- node [above] {psmin$=$psmout} ( $ (throttle.south west)!0.3!(throttle.west) $ )  ;% 
\path [draw, var] (manifil.east) -- node [above] {Tsmin$=$Tsmout} (throttle.west)  ;% 
     \draw [var] ( $ (inlet.south east)!0.5!(inlet.east) $ )  -- node [above,near start] {Tsmin$=$Tcpout} ( $ (manifil.south west)!0.5!(manifil.west) $ ) ;%
%### Outlet
%## Power Arrows 
%### Inlet
     \draw [Pf_in] ( $ (inlet.north east)!0.5!(inlet.east) $ )  -- node [above,near start] {Qsmin$=$Qcp} ( $ (manifil.north west)!0.5!(manifil.west) $ ) ;%
     \draw [Pe_out] (inlet.east)  -- node [above,near start] {psmin$=$pcpout} (manifil.west) ;%
%### Outlet     
     \draw [Pf_out] ( $ (manifil.north east)!0.3!(manifil.east) $ )  -- node [above] {Qsmout} ( $ (throttle.north west)!0.3!(throttle.west) $ ) ;%
% We could simply have written (manifil) .. (throttle.140). However, it's
% best to avoid hard coding coordinates
\draw [Pf_out] ( $ (throttle.north east)!0.60!(throttle.east) $ ) -- node [ann, pos=0.61] {Mass Flow Rate} + (\edgedist,0) 
        node[right] {Qsmout};%
\draw [Pf_out] ( $ (throttle.south east)!0.40!(throttle.east) $ ) -- node [ann, pos=0.61] {Fuel Cell Pressure} + (\edgedist,0) 
        node[right] {psmout$=p_{fc,in}$};%
\end{tikzpicture}
\end{figure}
\end{document}

So, the question is, what can I do to obtain a figure which will be automaticly adapt to the page size? Can I declare my "distance constants" (node distance, edge distance, text arrow pos, etc.) relatively compare to \textwidth for exemple?

EDIT 01/28/22: Answers below solve my picture problem. However I'm still wonder what are the "good practice" in matter of distance in a tikzpicture (absolute or relative). Wouldn't it be more rigorous if the distances were all given in terms of a distance like \textwidth or something? If nobody respond to this in the coming days I will accept one answer and so "close" this question. One day I may ask a question just about "good practice" regarding distances with a Tikzpicture, but currently I'm not sure this question makes sense (I'm new to Tikz)...

2
  • 1
    One option could be to produce the figure like an independent element with standalone class and include in your document with \includegraphics[width=\linewidth]{...}.
    – Ignasi
    Commented Jan 27, 2022 at 16:27
  • @Ignasi Thanks for your comment. Yes I know this technic but I never use it, I will surely use it sometime but currently I want the font and color to be auto adjusted to the document theme... Thanks again.
    – zetyty
    Commented Jan 27, 2022 at 21:06

2 Answers 2

1

The main problem here is the font size, not the page size. The beamer example is using 8pt as default font size while article uses 10pt.

(Try \documentclass[aspectratio=169,compress,10pt,svgnames,dvipsnames]{beamer})

(1) Add \footnotesize after \begin{figure}[h]

(2) Rescale the figure using for example

\begin{tikzpicture}[scale=1.2, every node/.style={transform shape}] 

c

For the default font size (10 points), the footnote size is 8 points. (10 points for 12 points normal size). The factor 1.2 roughly scales the footnotesize to the normal font size and the nodes at the same time.

Alternative for scaling

\scalebox{1.2}{ %
    \begin{tikzpicture}
       ...
\end{tikzpicture}

}

1
  • Well seen for the font size thank you! This solves my problem, however I'm still interesting in learning what are the "good practice" in matter of distance in a tikzpicture. Wouldn't it be more rigorous if the distances were all given in terms of a distance like \textwidth? If so, how can I achieve that ? Thanks a lot anyway !
    – zetyty
    Commented Jan 27, 2022 at 21:03
1

I would not scale image. By it you will lost consisteny between used fonts. Better is to prepare more version of image, which differ in selected font size. For example, one for article document, one for presentation by beamer.

I have to admit I got lost your the image code. So I wrote it again (from scratch):

\documentclass{article}
%\documentclass{beamer}
\usepackage[math]{iwona}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                backgrounds,
                fit,
                positioning,
                quotes}
%\usepackage{verbatim}
%\usepackage{bondgraphs}
%---------------- Show page layout. Don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
%\begin{frame}[fragile]
\begin{figure}
\centering
%\tiny or \scriptsize
    \begin{tikzpicture}[
node distance = 5mm and 20mm,
box/.style = {draw=teal, thick, rounded corners, fill=blue!30,
              minimum width=12mm, minimum height=18mm,
              align=center, text=red},
FIT/.style = {draw, densely dashed, rounded corners,fill=yellow!30,
              inner xsep=2.4mm, inner ysep=6mm, xshift=-1mm, yshift=-3mm,
              fit=#1},
arr/.style = {-Straight Barb},
arb/.style = {-{Straight Barb[right].Bar[line width=1.2pt]}, thick},
abr/.style = {{Bar[line width=1.2pt]}-{Straight Barb[right]}, thick},
every edge quotes/.style = {font=\linespread{.84}\selectfont, 
                            align=left, anchor=south, inner sep=1pt}
                    ]
% nodes
\node (n1)  [box]   {Manifold\\ Filling};
\node (n2)  [box, right=24mm of n1]   {Throtle};
%
\scoped[on background layer]
{
\node (n3)  [FIT=(n1) (n2),
             label = {[font=\bfseries, above right=3mm]south west:
                      supply manifold subsistem}]   {};
}
% arrows
\coordinate[above left=of n1.west] (aux1a);
\coordinate[below left=of n1.west] (aux1b);
%
\draw[arb]  (aux1a) to[near start, "Qsmin$=$Qcp"] (aux1a -| n1.west);
\draw[arb]  (aux1a |- n1.west) to[near start, "psmin$=$pcpout"] (n1);
\draw[arb]  (aux1b) to[near start, "Tsmin$=$Tcpout"] (aux1b -| n1.west);
%
\draw[abr]  (aux1a -| n1.east) to["Qsmout"] (aux1a -| n2.west);
\draw[arr]  (n1) to["Tsmin$=$Tsmout"] (n2);
\draw[arr]  (aux1b -| n1.east) to["psmin$=$psmout"] (aux1b -| n2.west);
%
\coordinate[above right=of n2.east] (aux2a);
\coordinate[below right=of n2.east] (aux2b);
%
\draw[abr]  (aux2a -| n2.east) 
    to["Mass Flow\\ Rate"] (aux2a) node[right] {Qsmout};
\draw[abr]  (aux2b -| n2.east) 
    to["Fuell Cell\\ Pressure"] (aux2b) node[right] {psmout$=p_{f_c,in}$};
    \end{tikzpicture}
\end{figure}
%\end{frame}
\end{document}

enter image description here

(red lines indicate text borders)

If this image is used in beamer presentation, in above MWE use lines with % on the beginning. Result will become:

enter image description here

2
  • Thanks, I like the idea of 2 versions because I often work on a latex document that I can compile as article or Beamer class by using different "packages files" and dedicated custom commands. So your solution is completly consistent with my practice. Also, I edited my question about the good practice of making a Tikzpicture... Thanks again !
    – zetyty
    Commented Jan 28, 2022 at 20:40
  • @SylvainRigal, instead saying "thanks" in spirit of policy of this site, you could upvote my answer. Regarding "what is good practice": this is very opinion based. Usual is the one in which you are the most skilled. I would stick with my proposition.
    – Zarko
    Commented Jan 28, 2022 at 22:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .