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.

  • 1
    Try anchor=west and .east instead of anchor=south west and .south east. – Paul Gaborit Jul 13 '16 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 Joshi Jul 13 '16 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. – Torbjørn T. Jul 13 '16 at 9:28
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

|improve this answer|||||
  • 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 Joshi Jul 13 '16 at 12:55
  • 1
    @RaviJoshi See section 17.5.3 Advanced Placement Options in the manual. – Torbjørn T. Jul 13 '16 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}
|improve this answer|||||
  • Thank you very much. I never knew about valign parameter. – Ravi Joshi Jul 13 '16 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. – Torbjørn T. Jul 13 '16 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}
|improve this answer|||||
  • Thanks Stefan. I am glad to see that there exists many ways to get the same thing done. Truly helpful. – Ravi Joshi Jul 13 '16 at 12:52

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.