我希望使用 TikZ 根據問題數量建立 3*x 表,如下圖所示。
我定義了一個方法,程式碼如下:
\newcounter{colnumber}
% Define Chinese numbers
\newcommand{\chinesenumber}[1]{%
\ifcase#1\or 一\or 二\or 三\or 四\or 五\or 六\or 七\or 八\or 九\or 十\fi
}
% Define the command to create the table
\newcommand{\createtable}[1]{%
\setcounter{colnumber}{#1} % Set the number of columns
% \pgfmathsetmacro{\cellwidth}{\textwidth/\thecolnumber} % Calculate the width of each cell
\begin{tikzpicture}[every node/.style={font=\fontsize{14}{18}\selectfont}]
\tikzset{cell/.style={rectangle, draw=black, minimum height=1.5cm, minimum width=2.8cm, align=center, inner sep=0pt}}
% Draw the column titles
\node[cell] at (2.8,0) {Num}; % First column title
\foreach \x in {2,...,\thecolnumber}
\node[cell] at (\x*2.8,0) {\chinesenumber{\numexpr\x-1}}; % Chinese number column titles
\node[cell] at (\thecolnumber*2.8+2.8,0) {Total score}; % Last column title
% Draw the rows
\node[cell] at (2.8,-1.5) {Score}; % First column second row
\node[cell] at (2.8,-3) {Teacher}; % First column third row
\foreach \y in {1,...,3}
\foreach \x in {2,...,\thecolnumber}
\node[cell] at (\x*2.8,-\y*1.5) {}; % Fill other cells
\foreach \y in {2,...,3}
\node[cell] at (\thecolnumber*2.8+2.8,-\y*1.5) {}; % Fill the last column's other cells
% Draw the outer frame
\draw (1.4,0.75) rectangle (\thecolnumber*2.8+4.2,-4.5); % Adjust size based on rows and columns
\end{tikzpicture}
}
\createtable{4}
當我在 main.tex 中使用命令 ( ) 時,我可以產生如下所示的表格。
然而,它似乎多了兩行。此外,當問題太多時,表格就會超出頁面範圍。我該如何解決這個問題?
答案1
宏\tblrbody
用 初始化\tl_new:N \tblrbody
。
內容收集為\tl_build_put_right:Nn
.過程以 開始\tl_build_begin:N
並以 結束\tl_build_end:N
。此後,將選項expand=\tblrbody
賦予放置的tblr
環境\tblrbody
。
行數取決於 後面的數字\int_step_inline:nn
。
\documentclass[border=6pt]{standalone}
\usepackage{tabularray}
\begin{document}
\ExplSyntaxOn
\tl_new:N \tblrbody
\tl_build_begin:N \tblrbody
\int_step_inline:nn { 4 }
{
\tl_build_put_right:Nn \tblrbody { #1 & & \\ }
}
\tl_build_end:N \tblrbody
\ExplSyntaxOff
\begin{tblr}[expand=\tblrbody]{
hlines,
vlines
}
Number & Score & Teacher\\
\tblrbody
Total score & & \\
\end{tblr}
\end{document}