我有一张表格。我需要在单元格中放入一些值(计数器的值),并根据单元格的位置对单元格应用某种格式。出于这个原因,我需要对单元格进行索引。
我使用代码进行索引:
\def\Cell#1#2{\csname dc#1#2\endcsname}
以及将信息放入单元格的代码
\def\SetCell#1#2#3{%
\expandafter\xdef\csname dc#1#2\endcsname{#3}
}
但是这样的代码扩展了 #3,这很好,因为我想使用计数器作为 #3,但如果我想格式化单元格(比如设置单元格颜色或更改字体),我会因为扩展而遇到问题。例如,我想用黄色装饰单元格 2,2,并且此单元格中的粗体字体有计数器编号 8。所以我的问题是:
是否有一个好的索引方法将格式化的数据(包括计数器值)传递到单元格?
\documentclass[]{article}
\usepackage{xcolor,colortbl}
\usepackage{hhline}
\usepackage{multirow, makecell}
\usepackage{xinttools}
\def\Cell#1#2{\csname dc#1#2\endcsname}
\def\SetCell#1#2#3{%
\expandafter\edef\csname dc#1#2\endcsname{#3}
}
\begin{document}
\newcounter{i}
\setcounter{i}{1}
\newcounter{j}
\setcounter{j}{1}
\romannumeral\xintreplicate{2}{%
\setcounter{j}{1}
\romannumeral\xintreplicate{8}{%
\SetCell{\thei}{\thej}{\thej}%
\stepcounter{j}%
}
\stepcounter{i}%
}
\begin{tabular}{|c||c|c||c|c|}
\hline
\multirow{3}{*}{1}%
& \Cell{1}{1} & \Cell{1}{2} & \Cell{1}{3} & \Cell{1}{4} \\ \hhline{|~||*2{-}*2{-}}
& \Cell{1}{5} & \Cell{1}{6} & \Cell{1}{7} & \Cell{1}{8} \\ \hhline{*3{-}*2{-}}
\multirow{3}{*}{2}%
& \Cell{2}{1} & \Cell{2}{2} & \Cell{2}{3} & \Cell{2}{4} \\ \hhline{|~||*2{-}*2{-}}
& \Cell{2}{5} & \Cell{2}{6} & \Cell{2}{7} & \Cell{2}{8} \\ \hhline{*3{-}*2{-}}
\end{tabular}
\end{document}
期望的结果是所需年份的表格(日历),月份从 9 到 12 或 2 到 5(完整代码这里):
答案1
我写了卢阿普罗格表包,它就是为此类场景设计的。使用 可以轻松设置单元格\LPTSetCell
。
以下代码生成表:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{luaprogtable}
\usepackage{array}
\usepackage{hhline}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{expl3}
\begin{document}
% create a new table named calendar
% which has 14 rows, 15 columns
\LPTNewTable{calendar}{15}{|c||*{7}{c|}|*{7}{c|}}[nrows=14, default after line=\cline{2-15}]
% bind current table
\LPTSetCurrentTable{calendar}
% set before line for row 1
\LPTSetRowProp{1}{before line=\hline}
% set double lines
\LPTSetRowProp{1,2,5, 8, 11}{after line=\hhline{*{15}{=}}}
\LPTSetRowProp{-1}{after line=\hline}
% set some individual cells
\LPTSetCell{1,1}{2020}
\LPTSetCell{1,2}[1,7]{\multicolumn{7}{c||}{\bfseries I Week}}
\LPTSetCell{1,9}[1,7]{\multicolumn{7}{c|}{\bfseries II Week}}
\LPTSetCell{2,1}{Day}
\LPTSetCell{3,1}[3,1]{\multirow{3}{*}{\rotatebox{90}{February}}}
\LPTSetCell{6,1}[3,1]{\multirow{3}{*}{\rotatebox{90}{March}}}
\LPTSetCell{9,1}[3,1]{\multirow{3}{*}{\rotatebox{90}{April}}}
\LPTSetCell{12,1}[3,1]{\multirow{3}{*}{\rotatebox{90}{May}}}
\ExplSyntaxOn
% fill day names
\clist_set:Nn \l_tmpa_clist {Mo,Tu,We,Th,Fr,Sa,Su}
\int_step_inline:nn {7} {
\exp_args:Nxx \LPTSetCell {2,\int_eval:n {#1+1}} {\clist_item:Nn \l_tmpa_clist {#1}}
\exp_args:Nxx \LPTSetCell {2,\int_eval:n {#1+8}} {\clist_item:Nn \l_tmpa_clist {#1}}
}
% function to fill days
% #1: start number
% #2: end number
% #3: offset
% #4: filling row index
\cs_set:Npn \fill_calendar:nnnn #1#2#3#4 {
% stores current row
\int_set:Nn \l_tmpa_int {#4}
% current column index
\int_set:Nn \l_tmpb_int {2 + #3}
\int_step_inline:nnn {#1} {#2} {
\exp_args:Nxx \LPTSetCell {\int_use:N \l_tmpa_int, \int_use:N \l_tmpb_int} {##1}
\int_incr:N \l_tmpb_int
\int_compare:nNnT {\l_tmpb_int} = {14+2} {
\int_incr:N \l_tmpa_int
\int_set:Nn \l_tmpb_int {2}
}
}
}
\fill_calendar:nnnn {3}{29}{0}{3}
\fill_calendar:nnnn {2}{31}{0}{6}
\fill_calendar:nnnn {1}{30}{2}{9}
\fill_calendar:nnnn {1}{31}{4}{12}
\ExplSyntaxOff
{
\renewcommand{\arraystretch}{1.5}
\LPTUseTable
}
\end{document}