4

I am trying to put few images in a canvas, in such a way so that they become horizontally aligned and vertically centered collage in tikz. Below is the sample code-

\documentclass{standalone}
\usepackage{graphicx}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \node[anchor=south west,inner sep=0cm, outer sep=0cm] (img1) at (0, 0, 0) {\includegraphics[height=2cm]{pc}};
    \node[anchor=south west,inner sep=0cm, outer sep=0cm] (img2) at (img1.south east){\includegraphics[height=1cm]{arrow}};
    \node[anchor=south west,inner sep=0cm, outer sep=0cm] (img3) at (img2.south east){\includegraphics[height=3cm]{db}};
    \node[anchor=south west,inner sep=0cm, outer sep=0cm] (img4) at (img3.south east){\includegraphics[height=1cm]{arrow}};
    \node[anchor=south west,inner sep=0cm, outer sep=0cm] (img5) at (img4.south east){\includegraphics[height=2cm]{pc}};

\end{tikzpicture}
\end{document}

This is how the generated pdf looks like- enter image description here

However, I want to the arrows vertically centered, something like this- enter image description here

Notice the alignment of the images in both the files.

3
  • 1
    Try anchor=west and .east instead of anchor=south west and .south east. Jul 13, 2016 at 6:16
  • @PaulGaborit: Du you mean \node[anchor=south east,inner sep=0cm, outer sep=0cm] (img3) at (img2.south west){\includegraphics[height=3cm]{db}}; ?
    – ravi
    Jul 13, 2016 at 7:55
  • 1
    No, he meant \node[anchor=west,inner sep=0pt,outer sep=0pt] (img3) at (img2.east) {\includegraphics{...}};. What you have done in your code is to place the lower left corner (anchor=south west) of the second image on the lower right corner (at (img1.south east)) of the first image, so naturally they're aligned at the bottom. Jul 13, 2016 at 9:28

3 Answers 3

4

Use positioning library. right = of horizontally aligns nodes by their center:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=0pt, outer sep=0pt}]
    \node (img1) {\includegraphics[height=2cm]{example-image}};
    \node[right=0pt of img1] (img2) {\includegraphics[height=1cm]{example-image}};
    \node[right=0pt of img2] (img3) {\includegraphics[height=3cm]{example-image}};
    \node[right=0pt of img3] (img4) {\includegraphics[height=1cm]{example-image}};
    \node[right=0pt of img4] (img5) {\includegraphics[height=2cm]{example-image}};

\end{tikzpicture}
\end{document}

enter image description here

2
  • Thanks. Appreciate it. I would like to know more about positioning library. Can you please tell me other than right = of, what are the other parameters available in this library?
    – ravi
    Jul 13, 2016 at 12:55
  • 1
    @RaviJoshi See section 17.5.3 Advanced Placement Options in the manual. Jul 13, 2016 at 13:19
3

If you don't have any other graphical elements then you don't need to use TikZ at all. The output of this is the same as Ignasi's code.

\documentclass{standalone}
\usepackage[export]{adjustbox} % provides valign-key for includegraphics
\begin{document}
\includegraphics[height=2cm,valign=c]{example-image}% end-of-line percent character removes space between images from line break in code
\includegraphics[height=1cm,valign=c]{example-image}%
\includegraphics[height=3cm,valign=c]{example-image}%
\includegraphics[height=1cm,valign=c]{example-image}%
\includegraphics[height=2cm,valign=c]{example-image}
\end{document}
2
  • Thank you very much. I never knew about valign parameter.
    – ravi
    Jul 13, 2016 at 12:51
  • 1
    @RaviJoshi As mentioned in the comment in the code, you need \usepackage[export]{adjustbox} to use that. If you just load graphicx, it is not available. Jul 13, 2016 at 13:17
1

A similar solution as Torbjørn, but without adjustbox, can be achieved using \raisebox

\documentclass[border=3mm]{standalone}
\usepackage{graphicx}
\begin{document}
\raisebox{-1.0cm}{\includegraphics[height=2cm]{example-image}}%
\raisebox{-0.5cm}{\includegraphics[height=1cm]{example-image}}%
\raisebox{-1.5cm}{\includegraphics[height=3cm]{example-image}}%
\raisebox{-0.5cm}{\includegraphics[height=1cm]{example-image}}%
\raisebox{-1.0cm}{\includegraphics[height=2cm]{example-image}}
\end{document}
1
  • Thanks Stefan. I am glad to see that there exists many ways to get the same thing done. Truly helpful.
    – ravi
    Jul 13, 2016 at 12:52

You must log in to answer this question.

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