1

I'm new to LaTeX and have a question about placing external graphics in table cells and align them correctly. Yes, I did read a similar posts but couldn't find the solution to my problem yet.

Here is my code (Please notice my usage of the TikZ. That is because I later want do draw arrows on top of my table.):

\documentclass[a4paper,headsepline]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,decorations.markings,decorations.pathreplacing,fadings,matrix,positioning}
\usepackage{booktabs}
\usepackage{array}
\begin{document}
\begin{table}[p]
\caption{title}
\begin{tikzpicture}[,inner sep=0,outer sep=0,on grid,x=.001\linewidth,y=.001\linewidth]
\node (tab) at (0,0) [align=center,anchor=south west,inner sep=0,outer sep=0] %I used TikZ because I want to draw on top of my table but the usage of TikZ is not the cause of my problem.
{
\begin{tabular}{r>{\raggedright\arraybackslash}p{2.5cm}*2{>{\centering\arraybackslash}p{5cm}}}
\toprule
\multicolumn{2}{l}{text text }&\multicolumn{2}{c}{text text text }\\
\cmidrule(r){1-2}\cmidrule(l){3-4}
&&A &B\\
\midrule
1&text text text text text &The pictures should be placed like this paragraph here but they have some unwanted space above them.&\raisebox{-\height}{\includegraphics[width=4cm]{../Grafiken/Platzhalter}}\\ %I've read about the \raisebox but that doesn't seem to help.
\midrule
&&caption A1, a caption with more than one row&caption B1\\
\midrule
2&In this table row the pictures are placed correctly but this text isn't. &\includegraphics[width=4cm]{../Grafiken/Platzhalter}    &\includegraphics[width=4cm]{../Grafiken/Platzhalter}\\ %Without the \raisebox the pictures are top-aligned but the text at the beginning of the row isn't.
\midrule
&&caption A2    &caption B2\\
\bottomrule
\end{tabular}
};
\begin{scope}[opacity=.75,transparency group]%just an example for what I want to do with TikZ
\draw[->,red,very thick] (500,10) to node[sloped, anchor=center, above] {This arrow is just an TikZ-example...} (800,350);
\end{scope}
\end{tikzpicture}
\end{table}
\end{document}

And here is the output I get: output

I want to place external images in my table cells but if I do so, there is unwanted space above them (row 1 in the table) or the text-filled cells behave weird (row 2). How do I fix this?

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Where is that similar post? Could you link to it? – LaRiFaRi Jul 1 '15 at 14:07
1

I would use the package adjustbox here. Please note the option valign=t I added to each image:

% arara: pdflatex

\documentclass[headsepline]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\PassOptionsToPackage{demo}{graphicx} % you can remove this.
\usepackage[export]{adjustbox}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{booktabs}
\usepackage{array}
\usepackage{ragged2e,microtype,lmodern} % some tweaks to get it really nice

\begin{document}
\begin{table}[p]
    \caption{title}
    \begin{tikzpicture}[,inner sep=0,outer sep=0,on grid,x=.001\linewidth,y=.001\linewidth]
        \node (tab) at (0,0) [align=center,anchor=south west,inner sep=0,outer sep=0]
        {
            \begin{tabular}{%
                r
                >{\RaggedRight}p{2.5cm}
                *2{>{\centering\arraybackslash}p{5cm}}
                }
            \toprule
            \multicolumn{2}{l}{text text} & \multicolumn{2}{c}{text text text} \\
            \cmidrule(r){1-2}\cmidrule(l){3-4}
            & & A & B \\
            \midrule
            1 & text text text text text & The pictures should be placed like this paragraph! Check & \includegraphics[valign=t,width=4cm]{../Grafiken/Platzhalter} \\
            \midrule
            & & caption A1, a caption with more than one row & caption B1 \\
            \midrule
            2 & In this table row the pictures are placed correctly. So is the text. & \includegraphics[valign=t,width=4cm]{../Grafiken/Platzhalter} & \includegraphics[valign=t,width=4cm]{../Grafiken/Platzhalter} \\ 
            \midrule
            & & caption A2 & caption B2 \\
            \bottomrule
            \end{tabular}
        };
        \begin{scope}[opacity=.75,transparency group]
        \draw[->,red,very thick] (500,10) to node[sloped, anchor=center, above] {This arrow is just a Ti\textit{k}Z-example\dots} (890,200);
        \end{scope}
    \end{tikzpicture}
\end{table}
\end{document}

enter image description here

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.