我在垂直居中“大”表格内容时遇到了问题。我使用表格,希望文本垂直和水平居中。你能帮我解决这个问题吗?
\documentclass[12pt,a4paper,twoside]{report}
%définition des packages
\usepackage{anyfontsize}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{helvet}
\usepackage[french]{babel}
\usepackage[top=1.5cm, bottom=1.5cm, left=2.5cm, right=1.5cm, headheight=1cm, headsep=2mm]{geometry}
\usepackage{amsmath}
\usepackage{tabularx}
\usepackage{amssymb}
\usepackage{fancyhdr}
\usepackage[pdftex]{graphicx}
\usepackage{url}
\usepackage[justification=centering]{caption}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage[Sonny]{fncychap}
\usepackage[tight]{minitoc}
\mtcselectlanguage{french}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{lscape}
\usepackage{array}
\usepackage{cellspace}
\usepackage{colortbl,xcolor}
\usepackage[table]{xcolor}
\usepackage[colorlinks=true,breaklinks=true,linkcolor=blue]{hyperref}
\usepackage{tcolorbox}
\begin{document}
\begin{landscape}
\begin{table}[!h]
\begin{center}
\caption{Blabla \protect}
\vspace*{0.5cm}
\begin{tabular}{|C{1cm}|C{2cm}|C{2cm}|C{2cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{2cm}|C{1.5cm}|C{2cm}|C{3cm}|}
\hline \rowcolor{lightgray} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & Image \\
\hline\hline
bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & \includegraphics[width=3cm,height=2.99cm]{sova.png} \\
\hline
bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & \includegraphics[width=3cm,height=2.99cm]{sova.png} \\
\hline
bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & \includegraphics[width=3cm,height=2.99cm]{sova.png} \\
\hline
\end{tabular}
\end{center}
\end{table}
\end{landscape}
\end{document}
答案1
另一种可能性是adjustbox
包,通过其指令\valign=m
将图像基线移动到图像的(垂直)中间。对于更短的代码,您也可以使用\adjustimage
od并在指令中为表中的所有图像includegraphics
定义图像选项:adjustboxset
\documentclass[12pt,a4paper,twoside]{report}
%définition des packages
\usepackage[T1]{fontenc}
\usepackage{helvet}
\usepackage[french]{babel}
\usepackage[vmargin=1.5cm,
hmargin={2.5cm,1.5cm},
headheight=1cm, headsep=2mm]{geometry}
\usepackage{array, amsmath, amssymb, cellspace, tabularx}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\usepackage{fancyhdr}
\usepackage[export]{adjustbox}
\usepackage[skip=1ex, justification=centering]{caption}
\usepackage{pdflscape}
\usepackage[table]{xcolor}
\usepackage[colorlinks=true,breaklinks=true,linkcolor=blue]{hyperref}
\begin{document}
\begin{landscape}
\begin{table}[!h]
\centering
\adjustboxset{width=\linewidth, valign=m, margin=0pt 3pt 0pt 3pt}
\setlength\extrarowheight{2pt}
\caption{Blabla \protect}
\begin{tabular}{|C{1cm}|C{2cm}|C{2cm}|C{2cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{2cm}|C{1.5cm}|C{2cm}|C{3cm}|}
\hline \rowcolor{lightgray} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & Image \\
\hline\hline
bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & \adjustimage{}{example-image-duck} \\
\hline
bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & \adjustimage{}{example-image-duck} \\
\hline
bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & \adjustimage{}{example-image-duck} \\
\hline
\end{tabular}
\end{table}
\end{landscape}
\end{document}
注意,您的前言中有许多包使用不同的选项加载多次。请再次检查前言并删除所有重复的包,然后看看您是否真的需要所有这些包。
答案2
如果您使用 定义图像列m{3cm}
,则文本条目将与图像的中心对齐。为了使图像列居中,我添加了 >{\centering\arraybackslash}
。我重新定义了C
列以执行此操作。
\documentclass[12pt,a4paper,twoside]{report}
%définition des packages
\usepackage{anyfontsize}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{helvet}
\usepackage[french]{babel}
\usepackage[top=1.5cm, bottom=1.5cm, left=2.5cm, right=1.5cm, headheight=1cm, headsep=2mm]{geometry}
\usepackage{amsmath}
\usepackage{tabularx}
\usepackage{amssymb}
\usepackage{fancyhdr}
\usepackage[pdftex]{graphicx}
\usepackage{url}
\usepackage[justification=centering]{caption}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage[Sonny]{fncychap}
\usepackage[tight]{minitoc}
\mtcselectlanguage{french}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{lscape}
\usepackage{array}
\usepackage{cellspace}
\usepackage{colortbl,xcolor}
\usepackage[table]{xcolor}
\usepackage[colorlinks=true,breaklinks=true,linkcolor=blue]{hyperref}
\usepackage{tcolorbox}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\begin{document}
\begin{landscape}
\begin{table}[!h]
\begin{center}
\caption{Blabla \protect}
\vspace*{0.5cm}
\begin{tabular}{|C{1cm}|C{2cm}|C{2cm}|C{2cm}|C{1cm}|C{1cm}|C{1cm}|C{1cm}|C{2cm}|C{1.5cm}|C{2cm}|C{3cm}|}
\hline \rowcolor{lightgray} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & Image \\
\hline\hline
bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & \includegraphics[width=3cm,height=2.99cm]{example-image} \\
\hline
bla & bla & bla bla bla bla bla bla bla bla & bla & bla & bla & bla & bla & bla & bla & bla & \includegraphics[width=3cm,height=2.99cm]{example-image} \\
\hline
bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & bla & \includegraphics[width=3cm,height=2.99cm]{example-image} \\
\hline
\end{tabular}
\end{center}
\end{table}
\end{landscape}
\end{document}