
(数式モードで)最初の列をarray
他の列より少し広くし、残りの列は一定の幅にしたいとします。次のように書くと、
\begin{array}{c|ccc}
10000 & 1 & 20 & 300
\end{array}
すると、2 列目、3 列目、4 列目の幅がすべて別々になりますが、これは避けたいものです。理想的には、最初の列の幅を に設定し2cm
、残りの列の幅を に設定します1cm
。
array
それを実行する方法はありますか? ネストされた環境を避けたいです。
答え1
私は、(a) 列 2、3、4 はすべて、いずれかの列の最も幅の広い要素 (提供された表の「300」) と同じ幅である必要があり、(b) これらのコンテンツの内容は中央揃えである必要があると想定しています。この解釈が正しい場合は、以下のコードで呼び出される新しい列タイプを定義して、C
これらの想定を満たし、その内容をデフォルトで数式モードにすることをお勧めします。
列 2、3、4 が実際に同じ幅であることがすぐにわかるように、コードに縦棒を追加しました。
\documentclass{article}
\newlength\mylen
\usepackage{array} % for "\newcolumntype" macro
\newcolumntype{C}{>{\hfil$}p{\mylen}<{$\hfil}} % centered, in math mode, fixed width
\begin{document}
\[
\settowidth\mylen{300} % choose widest element in columns 2--4
\begin{array}{|c|C|C|C|}
10000 & 1 & 20 & 300\\
1 & 101 & 555 & 888\\
\end{array}
\]
\end{document}
2021年6月更新: この回答を投稿してから 4 年以上経ちましたが、パッケージarray
は大幅に更新されています。更新の 1 つはw
列タイプの導入であり、これにより上記の設定は次のように簡潔に書き直すことができます。
\documentclass{article}
\newlength\mylen
\usepackage{array} % for 'w' column type
\begin{document}
\[
\settowidth\mylen{300} % choose widest element in columns 2--4
\begin{array}{| c | *{3}{wc{\mylen}|} }
10000 & 1 & 20 & 300\\
1 & 101 & 555 & 888
\end{array}
\]
\end{document}
答え2
可能性としてはdcolumn
、
\documentclass{article}
\usepackage{dcolumn,booktabs}
\begin{document}
\[
\begin{array}{r *{3}{D{.}{.}{3.-1}}}
\toprule
10000 & 1 & 20 & 300 \\
100 & 10 & 10 & 10 \\
\bottomrule
\end{array}
\]
\[
\begin{array}{r *{3}{D{.}{.}{3.-1}}}
\toprule
10000 & 100 & 200 & 300 \\
100 & 100 & 100 & 100 \\
\bottomrule
\end{array}
\]
\end{document}
2 番目の表は、間隔が同じであることを示しています。列の種類の引数はD
、それぞれ入力小数点区切り記号、出力小数点区切り記号、および数値の形式です。ここでは、3.-1
「整数部に 3 桁、小数部なし」を意味します。
答え3
\hphantom
2 列目から 4 列目の列幅を等しくするソリューション:
\documentclass{article}
\begin{document}
\[
\begin{array}{c|ccc}
10000 & \hphantom{00}1 & \hphantom{0}20 & 300
\end{array}
\]
\end{document}
列タイプp
は でも機能しますarray
。例:
\documentclass{article}
\usepackage{array}
\begin{document}
\[
\begin{array}{>{\centering}p{2cm}|*{3}{>{\centering}p{1cm}}}
10000 & 1 & 20 & 300
\end{array}
\]
\end{document}
またはパッケージで列を揃えますsiunitx
:
\documentclass{article}
\usepackage{siunitx}
\begin{document}
\[
\begin{array}{S[table-format=5]|*{3}{S[table-format=3]}}
10000 & 1 & 20 & 300
\end{array}
\]
\end{document}