有人能帮我居中吗?我本质上想让图案和表格中的文本居中。我会添加更多图案,也会调整图案大小。
我\tabularx
专门使用它,因为我知道如何合并表格的第一行。如果有更好的方法可以使用,我很乐意妥协。虽然尽可能简单的东西会很棒!
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage[thinlines]{easytable}
\usepackage{mathtools}
\usepackage{adjustbox}
\usepackage{array}
\usepackage{graphicx}
\usepackage{enumerate}
\usepackage{permute}
\usepackage{xparse}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{mdwlist}
\usepackage[hmargin = 1.25 in, bmargin=.8 in, tmargin=1.2in, headsep=0.2in]{geometry}
\usepackage{tabularx,colortbl}
\usepackage[table]{xcolor}
\usepackage{enumitem}
\usepackage{amsfonts}
\usepackage{tikz}
\usepackage{sectsty}
\usepackage{titlesec}
\usetikzlibrary{patterns}
\usetikzlibrary{patterns.meta}
\begin{document}
\begin{tabularx}{0.7\textwidth} { | >{\centering\arraybackslash}X | >{\centering\arraybackslash}X | >{\centering\arraybackslash}X|}
\hline
\multicolumn{3}{|c|}{Diagram Key} \\\hline
Pattern & Pattern Description & Area Type \\\hline
\begin{tikzpicture}\draw [draw=red, pattern={Lines[angle=45,distance=4pt]},pattern color=red] (0,0)--(0,1)--(1,1)--(1,0)--cycle;\end{tikzpicture} & $1$ & 7 \\\hline
$a$ & $2$ & 7 \\\hline
$b$ & $2$ & 7 \\\hline
$c$ & $6$ & 7 \\\hline
\end{tabularx}
\end{document}
答案1
从第一张截图可以看出,m
leandriis 提出的列确实运行良好。
\documentclass{article}
\usepackage{tabularx}
\usepackage{array}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{patterns.meta}
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}
\begin{tabularx}{0.7\textwidth}{|*{3}{>{\centering\arraybackslash}X|}}
\hline
\multicolumn{3}{|c|}{Diagram Key}\\
\hline
Pattern
& Pattern Description
& Area Type\\
\hline
\begin{tikzpicture}
\draw [
draw=red,
pattern={Lines[angle=45,distance=4pt]},
pattern color=red
] (0,0)--(0,1)--(1,1)--(1,0)--cycle;
\end{tikzpicture}
& 1
& Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua\\
\hline
$a$
& 2
& 7\\
\hline
$b$
& 2
& 7\\
\hline
$c$
& 6
& 7\\
\hline
\end{tabularx}
\end{document}
要修复间距问题,您可以将基线设置tikzpicture
为大于您看到的空白的任意值。0.5em
例如:
\documentclass{article}
\usepackage{tabularx}
\usepackage{array}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{patterns.meta}
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}
\begin{tabularx}{0.7\textwidth}{|*{3}{>{\centering\arraybackslash}X|}}
\hline
\multicolumn{3}{|c|}{Diagram Key}\\
\hline
Pattern
& Pattern Description
& Area Type\\
\hline
\begin{tikzpicture}[baseline=.5em]
\draw [
draw=red,
pattern={Lines[angle=45,distance=4pt]},
pattern color=red
] (0,0) rectangle (1,1);
\end{tikzpicture}
& 1
& 1\\
\hline
$a$
& 2
& 7\\
\hline
$b$
& 2
& 7\\
\hline
$c$
& 6
& 7\\
\hline
\end{tabularx}
\end{document}
编辑
OP 希望矩形前后有相等的空白。这可以通过在环境\vspace{.5em}
之前和之后添加来轻松实现tikzpicture
。为了获得更模块化的代码,您可以\vspace{.5em}
使用etoolbox
's\BeforeBeginEnvironment
和AfterEndEnvironment
命令将 添加到开始和结束文档挂钩中。理想情况下,您也只会在环境内部调用它,tabularx
以确保任何tikzpicture
表格外部的内容都不会受到此新间距的影响。
\documentclass{article}
% Packages
\usepackage{tabularx}
\usepackage{array}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{etoolbox}
% Libraries
\usetikzlibrary{patterns}
\usetikzlibrary{patterns.meta}
% Vertically center the X cells
\renewcommand\tabularxcolumn[1]{m{#1}}
% Add a vspace to the tikzpicture if inside a tabularx
\newtoggle{intabularx}
\togglefalse{intabularx}
\AtBeginEnvironment{tabularx}{
\toggletrue{intabularx}
}
\AtEndEnvironment{tabularx}{
\togglefalse{intabularx}
}
\BeforeBeginEnvironment{tikzpicture}{
\iftoggle{intabularx}{\vspace{.5em}}{}
}
\AfterEndEnvironment{tikzpicture}{
\iftoggle{intabularx}{\vspace{.5em}}{}
}
\begin{document}
\begin{tabularx}{0.7\textwidth}{|*{3}{>{\centering\arraybackslash}X|}}
\hline
\multicolumn{3}{|c|}{Diagram Key}\\
\hline
Pattern
& Pattern Description
& Area Type\\
\hline
\begin{tikzpicture}[baseline=.5em]
\draw [
draw=red,
pattern={Lines[angle=45,distance=4pt]},
pattern color=red
] (0,0) rectangle (1,1);
\end{tikzpicture}
& 1
& 1\\
\hline
$a$
& 2
& 7\\
\hline
$b$
& 2
& 7\\
\hline
$c$
& 6
& 7\\
\hline
\end{tabularx}
\end{document}
答案2
在第一个实现中可能会添加一些垂直间距,但你真正应该考虑第二个实现。
居中 atikzpicture
实际上很容易。
\documentclass{article}
\usepackage[hmargin = 1.25 in, bmargin=.8 in, tmargin=1.2in, headsep=0.2in]{geometry}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{patterns.meta}
\begin{document}
\begin{tabularx}{0.7\textwidth}{| *{3}{>{\centering\arraybackslash}X|}}
\hline
\multicolumn{3}{|c|}{Diagram Key} \\
\hline
Pattern & Pattern Description & Area Type \\
\hline
\begin{tikzpicture}[baseline=(current bounding box.west)]
\draw [draw=red, pattern={Lines[angle=45,distance=4pt]},pattern color=red]
(0,0)--(0,1)--(1,1)--(1,0)--cycle;
\end{tikzpicture}
& $1$ & 7 \\
\hline
$a$ & $2$ & 7 \\
\hline
$b$ & $2$ & 7 \\
\hline
$c$ & $6$ & 7 \\
\hline
\end{tabularx}
\bigskip
\begin{tabular*}{0.7\textwidth}{@{\extracolsep{\fill}}ccc@{}}
\toprule
\multicolumn{3}{c}{Diagram Key} \\
\midrule
Pattern & Pattern Description & Area Type \\
\midrule
\begin{tikzpicture}[baseline=(current bounding box.west)]
\draw [draw=red, pattern={Lines[angle=45,distance=4pt]},pattern color=red]
(0,0)--(0,1)--(1,1)--(1,0)--cycle;
\end{tikzpicture}
& $1$ & 7 \\
$a$ & $2$ & 7 \\
$b$ & $2$ & 7 \\
$c$ & $6$ & 7 \\
\bottomrule
\end{tabular*}
\end{document}
答案3
\renewcommand\tabularxcolumn[1]{m{#1}}
根据和,我建议如下cellspace
:
\documentclass{article}
\usepackage[hmargin = 1.25 in, bmargin=.8 in, tmargin=1.2in, headsep=0.2in]{geometry}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{patterns.meta}
\usepackage[column=0]{cellspace}
\setlength{\cellspacetoplimit}{0.5\tabcolsep}
\setlength{\cellspacebottomlimit}{\cellspacetoplimit}
\addparagraphcolumntypes{X}
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}
\begin{tabularx}{0.7\textwidth}{| *{3}{>{\centering\arraybackslash}0{X}|}}
\hline
\multicolumn{3}{|0c|}{Diagram Key} \\
\hline
Pattern & Pattern Description & Area Type \\
\hline
\begin{tikzpicture}
\draw [draw=red, pattern={Lines[angle=45,distance=4pt]},pattern color=red]
(0,0)--(0,1)--(1,1)--(1,0)--cycle;
\end{tikzpicture}
& $1$ & 7 \\
\hline
$a$ & $2$ & 7 \\
\hline
$b$ & $2$ & 7 \\
\hline
$c$ & $6$ & 7 \\
\hline
\end{tabularx}
\end{document}