表格中的自由單元格對齊

表格中的自由單元格對齊

我想使用tabu我聽說的最新和最好的用於創建表的包來生成如下所示的表:

-------------------------------------
|A          |     B     |          C| 
|           |           |           | 
|           |           |           | 
-------------------------------------
|           |           |           | 
|A          |     B     |          C| 
|           |           |           | 
-------------------------------------
|           |           |           | 
|           |           |           | 
|A          |     B     |          C| 
-------------------------------------

這是一個簡單的表格,但每個單元格中的文字對齊方式不同。套件文檔說X列可以同時垂直對齊(使用p, m, b)和水平對齊(使用l, c, r, j),但我找不到使用此功能來建立此表的方法。我知道總是可以使用一些低階命令,例如\parbox用於此目的,但我很想知道是否有更好的方法來做到這一點。

如果有任何其他套件可以創建這樣的表或幫助對齊,請在此處討論。

答案1

tabu使用危險的包裝;它有很多錯誤並且沒有維護;此外,新版本將與目前版本不相容。我目前的建議是不要使用tabu(參見this threadctt)。

您可以簡單地使用array包來聲明一些列類型,然後\multicolumn可以幫助從一種類型切換到另一種類型:

\documentclass{article}
\usepackage{array}

\newcolumntype{L}[4]{
  >{\vspace{0pt}\minipage[c][#2][#3]{3cm}%
      \ifx#4r\relax\raggedright
        \else
      \ifx#4l\relax\raggedleft
        \else
      \ifx#4c\relax\centering
      \fi\fi\fi
    }p{#1}
  <{\endminipage\vspace{\tabcolsep}}}

\begin{document}

\noindent\begin{tabular}{|L{3cm}{3cm}{t}{r}|L{3cm}{3cm}{t}{c}|L{3cm}{3cm}{t}{l}|}
\hline
A  & B & C \tabularnewline
\hline
\multicolumn{1}{|L{3cm}{3cm}{c}{r}|}{A} 
& \multicolumn{1}{L{3cm}{3cm}{c}{c}|}{B} 
& \multicolumn{1}{L{3cm}{3cm}{c}{l}|}{C} 
\tabularnewline
\hline
\multicolumn{1}{|L{3cm}{3cm}{b}{r}|}{A} 
& \multicolumn{1}{L{3cm}{3cm}{b}{c}|}{B} 
& \multicolumn{1}{L{3cm}{3cm}{b}{l}|}{C} 
\tabularnewline
\hline
\end{tabular}

\end{document}

在此輸入影像描述

列類型的語法L

L{<width>}{<height>}{<v. position>}{<text justification>}

其中<width><height>控制單元格的寬度和高度,<v. position>是單元格內文本的垂直位置(可能的值:ttop、ccenter、b...、bottom),並<text justification>控制單元格內文本的對齊方式(可能的值:lraggedleft、ccentered、rraggedright )。

答案2

這是一個基於 TikZ 的答案,我根據 Roberto Bonvallet 的一些 Sudoko 生成程式碼進行了改編(來源:http://www.texample.net/tikz/examples/sudoku/)。

% Original author: Roberto Bonvallet
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\newcounter{row}
\newcounter{col}

\newcommand\setrow[9]{
  \setcounter{col}{1}
  \foreach \n in {#1, #2, #3, #4, #5, #6, #7, #8, #9} {
    \edef\x{\value{col} - 0.5}
    \edef\y{9.5 - \value{row}}
    \node[anchor=center] at (\x, \y) {\n};
    \stepcounter{col}
  }
  \stepcounter{row}
}

\begin{document}
  \begin{tikzpicture}
    \draw[scale=3] (0, 0) grid (3, 3);

    \setcounter{row}{1}
    \setrow {A}{ }{ }  { }{B}{ }  { }{ }{C}
    \setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
    \setrow { }{ }{ }  { }{ }{ }  { }{ }{ }

    \setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
    \setrow {A}{ }{ }  { }{B}{ }  { }{ }{C}
    \setrow { }{ }{ }  { }{ }{ }  { }{ }{ }

    \setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
    \setrow { }{ }{ }  { }{ }{ }  { }{ }{ }
    \setrow {A}{ }{ }  { }{B}{ }  { }{ }{C}
  \end{tikzpicture}
\end{document}

在此輸入影像描述

答案3

我建議包cals。文件"使用範例" 第 3 頁顯示了您想要的表格。

在此輸入影像描述

\documentclass{article}
\usepackage{xcolor,cals}% http://ctan.org/pkg/{xcolor,cals}
\begin{document}
\makeatletter
\newcommand\bbrow{\brow\setbox\cals@current@row=\hbox{\vbox to 3cm{}}}
\newenvironment{decotable}{\begin{calstable}
  \Large\cals@setpadding{Ag}\cals@setcellprevdepth{Al}
  \cals@paddingL=0pt \relax
  \cals@paddingR=0pt \relax
  \def\cals@framecs@width{2pt}
  \def\cals@rs@width{8pt}
  \def\cals@cs@width{4pt}
  }{\end{calstable}}

\begin{decotable}
  \colwidths{{3cm}{3cm}{3cm}}
  \bbrow
  \cell{left,\\top}
  \alignC \cell{center,\\top}
  \def\cals@borderL{0pt}\def\cals@borderB{0pt}
  \alignR \cell{right,\\top} \erow
  \let\cals@borderL=\relax \let\cals@borderB=\relax
  \bbrow
  \alignL \cell{\vfil left,\\middle}
  \alignC \cell{\vfil center,\\middle}
  \alignR \cell{\vfil right,\\middle} \erow
  \bbrow
  \def\cals@bgcolor{green}
  \alignL \cell{\vfill left,\\bottom}
  \def\cals@bgcolor{}
  \alignC \cell{\vfill center,\\bottom}
  \alignR \cell{\vfill right,\\bottom} \erow
\end{decotable}
\end{document}

相關內容