3

To draw a border around an image, I'm using rectangle from TikZ. Within the rectangle, I want to label the diagram. But the image is such that it occupies the rectangle's full space, so there is no space left to write the label. If I increase inner sep, it adds space in all 4 directions, which I don't want to happen. I want to add extra space at the bottom only. If I set a minimum height for the rectangle, then the image gets centered, and so there will be extra space at the top. I don't want this to happen either. I couldn't find an option to align the image to the bottom of the rectangle. Any help is appreciated.

PS: Currently, I'm adding an empty rectangle and then adding the image separately. Due to this, the inner sep for the rectangle will be zero. I've three similar diagrams, and I've to set inner sep for the other two rectangles also to zero. As a result, for the first diagram, a block within the diagram overlaps with the border rectangle.

% Contains figures for the presentation
%! Author = Nagabhushan

% Preamble
\documentclass[11pt]{article}

% Packages
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{makecell}
\usepackage{media9}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.17}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, positioning, calc, decorations.pathreplacing, external}
\tikzexternalize[prefix=figures/]
\newcommand{\etal}{\textit{et al.}}

% --------------------------- TikZ blocks - Start --------------------------------%
\tikzstyle{container} = [rectangle, rounded corners, draw=black, dashed]
\tikzstyle{circle-label} = [circle, text centered, draw=black]
% --------------------------- TikZ blocks - End --------------------------------%

% Document
\begin{document}
    \tikzsetnextfilename{Model_BlockDiagrams}
    \begin{tikzpicture}[node distance=2cm]
        \node (main) [container, inner sep=0] at (current page.north) {\includegraphics[width=\linewidth]{../../images/MainArchitecture_BlockDiagram.pdf}};
        \node (mcs) [container, below of=main, yshift=-4.5cm, inner sep=0] {\includegraphics[width=\linewidth]{../../images/MCS_BlockDiagram.pdf}};
        \node (rfd) [container, below of=mcs, minimum height=4.5cm, minimum width=\linewidth, yshift=-4cm] {};
        \node (rf-image) [below of=rfd, yshift=2.5cm]{\includegraphics[width=\linewidth]{../../images/RFD_BlockDiagram.pdf}};
        \node [circle-label, align=center, below of=main, right of=main, xshift=3.5cm] {A};
        \node [circle-label, align=center, below of=mcs, right of=mcs, xshift=3.5cm, yshift=-0.3cm] {B};
        \node [circle-label, align=center, below of=rfd, right of=rfd, xshift=3.5cm, yshift=0.3cm] {C};
    \end{tikzpicture}
    \newpage
\end{document}

Output:

Output

2

Like this?

enter image description here

I made few changes to your MWE:

  • instead obsolete \tikzstyle is now used \tikzset
  • for container is used node, which by use of TikZ library fit encapsulate your image and image label
  • first is insert image, then follows image labels in circle-label node, and on the end both are encapsulated in container using fit library
  • for positioning of nodes are used relative coordinates as they are define in positioning library
\documentclass[11pt]{article}

% Packages
\usepackage{amsmath}
\usepackage[demo]{graphicx}
\usepackage{makecell}
\usepackage{media9}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{%external,
                fit,
                positioning}
%\tikzexternalize[prefix=figures/]
%\newcommand{\etal}{\textit{et al.}}

% --------------------------- TikZ blocks - Start --------------------------------%
\tikzset{
   container/.style = {rectangle, rounded corners, draw, dashed,
                       fit=#1, inner sep=2mm, node contents={}},
circle-label/.style = {circle, draw}
        }
% --------------------------- TikZ blocks - End --------------------------------%

% Document
\begin{document}
    \begin{tikzpicture}[node distance=4mm and 3mm]
\node (g1) [inner sep=0pt] {\includegraphics[width=\linewidth]{../../images/MainArchitecture_BlockDiagram.pdf}};
\node (cl-1)    [circle-label, below left=of g1.south east] {A};
\node (c1) [container=(g1) (cl-1)];

\node (g2) [inner sep=0pt, below=of c1] {\includegraphics[width=\linewidth]{../../images/MainArchitecture_BlockDiagram.pdf}};
\node (cl-2)    [circle-label, below left=of g2.south east] {B};
\node (c2) [container=(g2) (cl-2)];

\node (g3) [inner sep=0pt, below=of c2] {\includegraphics[width=\linewidth]{../../images/MainArchitecture_BlockDiagram.pdf}};
\node (cl-3)    [circle-label, below left=of g3.south east] {C};
\node (c3) [container=(g3) (cl-3)];
    \end{tikzpicture}
    \newpage
\end{document}
5
  • Thanks. That worked! However, my exact requirement is slightly different. For the first two diagrams, I don't want to put the lab below the figure. This is because for the first two diagrams there is some empty space at the bottom right. I'll place my label there. But for the third diagram, I want to add extra space. Can you tell me how I can modify your code to achieve this? – Nagabhushan S N Oct 24 '20 at 17:13
  • 1
    @NagabhushanSN, if I understand you correct, than at the first two image you need to delete \node (cl-1) [circle-label, below left=of g1.south east] {...} and delete its name in fit option in ˙container` node, for example \node (c1) [container=(g1) (cl-1)]; – Zarko Oct 24 '20 at 17:35
  • I deleted passing cl-1, cl-2 to container and added yshift to nodes cl-1, cl-2 and it worked. Full code: \node (g1) [inner sep=0pt] {\includegraphics[width=\linewidth]{../../images/MainArchitecture_BlockDiagram.pdf}}; \node (cl-1) [circle-label, below left=of g1.south east, yshift=1.2cm] {A}; \node (c1) [container=(g1)]; – Nagabhushan S N Oct 25 '20 at 10:15
  • Thanks for this. Also can you elaborate on the usage of \tikzset instead of \tikzstyle or any links for the same? – Nagabhushan S N Oct 25 '20 at 10:17
  • 1
    @NagabhushanSN, tikzstyle is considered to be deprecated. See TikZ & PGF Manula`. It is part of your LaTeX installation, also you can find on Internet using Google :-) – Zarko Oct 25 '20 at 11:55
0

Based on this answer, I modified my code as follows and it worked. The crux is that the extra space required can be added by using the trim option in \includegraphics and providing negative values for the bottom. Like \includegraphics[width=\linewidth, trim=0 -1cm 0 0]{../../images/RFD_BlockDiagram.pdf}

Modified code:

% Contains figures for the presentation
%! Author = Nagabhushan

% Preamble
\documentclass[11pt]{article}

% Packages
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{makecell}
\usepackage{media9}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.17}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, positioning, calc, decorations.pathreplacing, external}
\tikzexternalize[prefix=figures/]
\newcommand{\etal}{\textit{et al.}}

% --------------------------- TikZ blocks - Start --------------------------------%
\tikzstyle{container} = [rectangle, rounded corners, draw=black, dashed]
\tikzstyle{circle-label} = [circle, text centered, draw=black]
% --------------------------- TikZ blocks - End --------------------------------%

% Document
\begin{document}
    \tikzsetnextfilename{Model_BlockDiagrams}
    \begin{tikzpicture}[node distance=2cm]
        \node (main) [container] at (current page.north) {\includegraphics[width=\linewidth]{../../images/MainArchitecture_BlockDiagram.pdf}};
        \node (mcs) [container, below of=main, yshift=-4.5cm] {\includegraphics[width=\linewidth]{../../images/MCS_BlockDiagram.pdf}};
        \node (rfd) [container, below of=mcs, yshift=-3.65cm] {\includegraphics[width=\linewidth, trim=0 -1cm 0 0]{../../images/RFD_BlockDiagram.pdf} };
        \node [circle-label, align=center, below of=main, right of=main, xshift=3.5cm] {A};
        \node [circle-label, align=center, below of=mcs, right of=mcs, xshift=3.5cm, yshift=-0.3cm] {B};
        \node [circle-label, align=center, below of=rfd, right of=rfd, xshift=3.9cm, yshift=0.4cm] {C};
    \end{tikzpicture}
    \newpage
\end{document}

Method2: Based on Zarko's answer, the below works too!

% Contains figures for the presentation
%! Author = Nagabhushan

% Preamble
\documentclass[11pt]{article}

% Packages
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{makecell}
\usepackage{media9}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.17}
\usepackage{tikz}
\usetikzlibrary{shapes, arrows, positioning, calc, decorations.pathreplacing, external, fit}
\tikzexternalize[prefix=figures/]
\newcommand{\etal}{\textit{et al.}}

% --------------------------- TikZ blocks - Start --------------------------------%
\tikzset{
container/.style = {rectangle, rounded corners, draw, dashed, fit=#1, inner sep=2mm, node contents={}},
circle-label/.style = {circle, draw}
}
% --------------------------- TikZ blocks - End --------------------------------%

% Document
\begin{document}
    \tikzsetnextfilename{Model_BlockDiagrams}
    \begin{tikzpicture}[node distance=4mm and 3mm]
        \node (g1) [inner sep=0pt] {\includegraphics[width=\linewidth]{../../images/MainArchitecture_BlockDiagram.pdf}};
        \node (cl-1)    [circle-label, below left=of g1.south east, yshift=1.2cm] {A};
        \node (c1) [container=(g1)];

        \node (g2) [inner sep=0pt, below=of c1] {\includegraphics[width=\linewidth]{../../images/MCS_BlockDiagram.pdf}};
        \node (cl-2)    [circle-label, below left=of g2.south east, yshift=1.2cm] {B};
        \node (c2) [container=(g2)];

        \node (g3) [inner sep=0pt, below=of c2] {\includegraphics[width=\linewidth]{../../images/RFD_BlockDiagram.pdf}};
        \node (cl-3)    [circle-label, below left=of g3.south east] {C};
        \node (c3) [container=(g3) (cl-3)];
    \end{tikzpicture}
    \newpage
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.