我是 LaTeX 新手,对如何将外部图形放入表格单元格并正确对齐它们有疑问。是的,我确实读过类似的帖子,但还没有找到解决问题的方法。
这是我的代码(请注意我使用了 Ti钾Z. 这是因为我后来想在桌子上画箭头。):
\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}
下面是我得到的输出:
我想在表格单元格中放置外部图像,但如果这样做,它们上方会出现不需要的空间(表格中的第 1 行)或文本填充的单元格行为异常(第 2 行)。我该如何修复这个问题?
答案1
我会在这里使用该包。请注意我添加到每个图像的adjustbox
选项:valign=t
% 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}