
Latex でこのような表を作成したいと思います。
これはテーブルを作成するためのスクリプトです。
\begin{table}
\centering
\caption{Table}
\begin{tabular}{|c|c|l|} \hline
\multirow{3}{*}{DEF}&\multicolumn{3}{|c|}{ ABC} \\ \hline
A&B&C\\ \hline
1& 4 & 7 \\ \hline
2& 5 & 8 \\ \hline
3& 6 & 9 \\ \hline
\end{tabular}
\end{table}
このスクリプトを実行すると、次のようなエラーが表示されます。
! Extra alignment tab has been changed to \cr.<template> \endtemplate ...row{3}{}{DEF}&\multicolumn{3}{|c|}{ ABC}
この画像のような表を作成するにはどうすればいいでしょうか?
答え1
次のテンプレートを解読できます。
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{array,multirow}
\let\mc=\multicolumn
\let\mr=\multirow
\let\cl=\cline
\begin{document}
\begin{tabular}{|*6{c|}l}
\cl{3-6}
\mc{2}{c|}{\mr{2}{*}{empty}} & \mc{4}{c|}{Primes} \\\cl{3-6}
\mc{2}{c|}{} & 2 & 3 & 5 & 7 \\\cl{1-6}
\mr{2}{*}{Powers} & 504 & 3 & 2 & 0 & 1 \\\cl{2-6}
& 540 & 2 & 3 & 1 & 0 \\\cl{1-6}
\mr{2}{*}{Powers} & HCF & 2 & 2 & 0 & 0 & min \\\cl{2-6}
& LCM & 3 & 3 & 1 & 1 & max \\\cl{1-6}
\end{tabular}
\end{document}
警告
\mr
ネストすることはできます\mc
が、その逆はできません。
参考文献
上記のコードは、以下の例からヒントを得ました。ウィキペディア次のように。
\begin{tabular}{cc|c|c|c|c|l}
\cline{3-6}
& & \multicolumn{4}{ c| }{Primes} \\ \cline{3-6}
& & 2 & 3 & 5 & 7 \\ \cline{1-6}
\multicolumn{1}{ |c }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{504} & 3 & 2 & 0 & 1 & \\ \cline{2-6}
\multicolumn{1}{ |c }{} &
\multicolumn{1}{ |c| }{540} & 2 & 3 & 1 & 0 & \\ \cline{1-6}
\multicolumn{1}{ |c }{\multirow{2}{*}{Powers} } &
\multicolumn{1}{ |c| }{gcd} & 2 & 2 & 0 & 0 & min \\ \cline{2-6}
\multicolumn{1}{ |c }{} &
\multicolumn{1}{ |c| }{lcm} & 3 & 3 & 1 & 1 & max \\ \cline{1-6}
\end{tabular}
しかし、見て比較すると、私のコードははるかにシンプルで、適切にリファクタリングされていても、読みやすいですよね?
答え2
あなたがやろうとしていることは、次のようにして実行できます:
\documentclass{standalone}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|c|c} \hline
& & \multicolumn{3}{|c|}{ ABC} \\ \hline
& & A&B&C\\ \hline
\multirow{3}{*}{DEF} & D &1& 4 & 7 \\ \cline{2-5}
&E& 2& 5 & 8 \\ \cline{2-5}
&F& 3& 6 & 9 \\ \cline{2-5}
\end{tabular}
\end{document}
しかし、LaTeX では、この Excel スタイルのレイアウトよりもはるかに見栄えの良い表を作成できます。3 列以上ある場合でも見栄えのよい、次のような表を作成することを検討してください。
\documentclass{standalone}
\usepackage{multirow}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{lcccccc}
\toprule
& \multicolumn{3}{c}{ ABC} & \multicolumn{3}{c}{ ABC} \\\cmidrule(lr){2-4} \cmidrule(lr){5-7}
& A&B&C & A2&B2&C2\\ \midrule
DEF:\\
\;D &1& 4 & 7 &1& 4 & 7 \\
\;E& 2& 5 & 8 &1& 4 & 7 \\
\;F& 3& 6 & 9 &1& 4 & 7 \\
XYZ:\\
\;X &1& 4 & 7 &1& 4 & 7 \\
\;Y& 2& 5 & 8 &1& 4 & 7 \\
\;Z& 3& 6 & 9 &1& 4 & 7 \\ \bottomrule
\end{tabular}
\end{document}
答え3
\multirow
コマンドは、独自の列に対して動作する必要があります...したがって、各列の左側に列を追加し、&
テーブルの各行の左側に追加の列を追加し、表のc
引数の先頭に追加の列タイプ (とします) を追加する必要があります。
また、例の複数列は 3 行の高さで、行 (1,4,7) から始まる必要があります。ただし、この行または次の行のいずれかで、"複数行" セルにも行が追加されますが、これは望ましくありません。したがって、 1 列目から最後の列ではなく、2 列目から 4 列目までの行に\hline
変更する必要があります。\hline
\cline{2-4}
最後に、最初の 2 行の最初の要素として空を追加して、行の動作をから\multicolumn{1}{c|}{}
に変更し、左側から不要な垂直線を削除します。|c|
c|
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}
\centering
\caption{Table}
\begin{tabular}{|c|c|c|l|} \cline{2-4}
\multicolumn{1}{c|}{}&\multicolumn{3}{|c|}{ ABC} \\ \cline{2-4}
\multicolumn{1}{c|}{}&A&B&C\\ \hline
\multirow{3}{*}{DEF}&1& 4 & 7 \\ \cline{2-4}
&2& 5 & 8 \\ \cline{2-4}
&3& 6 & 9 \\ \hline
\end{tabular}
\end{table}
\end{document}
追伸: あなたの間違いを示そうとしたのではなく、あなたのテーブルをより美しく見せる方法を見つけようとしたのです。