我试图自己解决这个问题,但我的技能不足以解决它。
答案1
TikZ 在这方面有一个优势。一旦定义了适当的样式,矩阵的语法就可以变得非常简单:
\matrix[options...]
{
Learning unit 1 & & &X&X&X& & \\
Learning unit 2 & & &X&X&X& & \\
Learning unit 3 &X&X&X& &X&X& \\
Learning unit 4 & & &X&X&X&X& \\
Learning unit 5 & & &X&X&X& & \\
Learning unit 6 & & &X&X&X&X&X\\
Learning unit 7 &X&X&X&X&X&X&X\\
Learning unit 8 &X&X& & &X&X& \\
Learning unit 9 &X&X& & &X&X&X\\
Learning unit 10 &X&X& & &X&X& \\
Learning unit 11 &X& & & &X& & \\
Learning unit 12 & & & &X&X&X&X\\
Learning unit 13 & & & &X& &X&X\\
Learning unit 14 &X& & &X&X& & \\
Learning unit 15 &X& & &X&X& & \\
};
诀窍是使用column 2
、column 3
等样式将背景颜色和前景色设置为所需的“块”颜色,这样那些“X”就不可见了。
这是完整的代码:
\usetikzlibrary{matrix}
\begin{tikzpicture}
% Define appropiate colors for each block
\colorlet{block1}{green!60!yellow!80!black}
\colorlet{block2}{blue!80!red!70!black}
\colorlet{block3}{orange}
\colorlet{block4}{blue!50!cyan}
\colorlet{block5}{orange!50!red}
\colorlet{block6}{yellow}
\colorlet{block7}{black!30}
% This style sets the background and foreground color to #1,
% and fixes the size of the "X" cells
\tikzset{
block/.style={#1, nodes={fill=#1, minimum height=4.5mm, minimum width=4.5mm}}
}
% Now the matrix
\matrix (M) [
inner sep=0pt,
matrix of nodes,
nodes={inner sep=0pt},
column 1/.style={nodes={ % Column 1 is different
text width=4cm, minimum height=4mm, inner sep=2pt,
font=\sffamily\scriptsize,
}},
column 2/.style={block=block1},
column 3/.style={block=block2},
column 4/.style={block=block3},
column 5/.style={block=block4},
column 6/.style={block=block5},
column 7/.style={block=block6},
column 8/.style={block=block7},
]
{
Learning unit 1 & & &X&X&X& & \\
Learning unit 2 & & &X&X&X& & \\
Learning unit 3 &X&X&X& &X&X& \\
Learning unit 4 & & &X&X&X&X& \\
Learning unit 5 & & &X&X&X& & \\
Learning unit 6 & & &X&X&X&X&X\\
Learning unit 7 &X&X&X&X&X&X&X\\
Learning unit 8 &X&X& & &X&X& \\
Learning unit 9 &X&X& & &X&X&X\\
Learning unit 10 &X&X& & &X&X& \\
Learning unit 11 &X& & & &X& & \\
Learning unit 12 & & & &X&X&X&X\\
Learning unit 13 & & & &X& &X&X\\
Learning unit 14 &X& & &X&X& & \\
Learning unit 15 &X& & &X&X& & \\
};
% Now draw the row lines
\foreach \i in {1,...,14}
\draw[gray] (M-\i-1.south west) -- (M-\i-1.south-|M.east);
% And the frame around the matrix
\draw[thick] (M.south west) rectangle (M.north east);
\end{tikzpicture}
结果:
更新
第 1 列的样式可以移动到新的 tikz 样式,这允许我们可以将其重新用于其他“图例”表。
这是两个表的完整代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\colorlet{block1}{green!60!yellow!80!black}
\colorlet{block2}{blue!80!red!70!black}
\colorlet{block3}{orange}
\colorlet{block4}{blue!50!cyan}
\colorlet{block5}{orange!50!red}
\colorlet{block6}{yellow}
\colorlet{block7}{black!30}
\tikzset{
block/.style={#1, nodes={fill=#1, minimum height=4.5mm, minimum width=4.5mm}},
headings/.style= {nodes={
text width=4cm, minimum height=4mm, inner sep=2pt,
font=\sffamily\scriptsize}
},
}
\begin{tikzpicture}
\matrix (M) [ % Legend table
inner sep = 0pt,
matrix of nodes,
nodes={inner sep=0pt},
column 1/.style=headings,
row 1 column 2/.style={block=block1},
row 2 column 2/.style={block=block2},
row 3 column 2/.style={block=block3},
row 4 column 2/.style={block=block4},
row 5 column 2/.style={block=block5},
row 6 column 2/.style={block=block6},
row 7 column 2/.style={block=block7},
] {
Block 1. Lorem ipsum &X\\
Block 2. Lorem ipsum &X\\
Block 3. Lorem ipsum &X\\
Block 4. Lorem ipsum &X\\
Block 5. Lorem ipsum &X\\
Block 6. Lorem ipsum &X\\
Block 7. Lorem ipsum &X\\
};
\foreach \i in {1,...,6}
\draw[gray] (M-\i-1.south west) -- (M-\i-1.south-|M.east);
\draw[thick] (M.south west) rectangle (M.north east);
\end{tikzpicture}
%
\hskip 1cm
%
\begin{tikzpicture} % Second table
\matrix (M) [
inner sep=0pt,
matrix of nodes,
nodes={inner sep=0pt},
column 1/.style=headings,
column 2/.style={block=block1},
column 3/.style={block=block2},
column 4/.style={block=block3},
column 5/.style={block=block4},
column 6/.style={block=block5},
column 7/.style={block=block6},
column 8/.style={block=block7},
]
{
Learning unit 1 & & &X&X&X& & \\
Learning unit 2 & & &X&X&X& & \\
Learning unit 3 &X&X&X& &X&X& \\
Learning unit 4 & & &X&X&X&X& \\
Learning unit 5 & & &X&X&X& & \\
Learning unit 6 & & &X&X&X&X&X\\
Learning unit 7 &X&X&X&X&X&X&X\\
Learning unit 8 &X&X& & &X&X& \\
Learning unit 9 &X&X& & &X&X&X\\
Learning unit 10 &X&X& & &X&X& \\
Learning unit 11 &X& & & &X& & \\
Learning unit 12 & & & &X&X&X&X\\
Learning unit 13 & & & &X& &X&X\\
Learning unit 14 &X& & &X&X& & \\
Learning unit 15 &X& & &X&X& & \\
};
\foreach \i in {1,...,14}
\draw[gray] (M-\i-1.south west) -- (M-\i-1.south-|M.east);
\draw[thick] (M.south west) rectangle (M.north east);
\end{tikzpicture}
\end{document}
结果:
答案2
也许不像 JLDiaz 的答案那样灵活,但只是作为非 tikz 的替代方案:
\documentclass[border=5]{standalone}
\renewcommand\familydefault\sfdefault
\usepackage{xcolor}
\usepackage{array}
\setlength\extrarowheight{1ex}
\usepackage{colortbl}
\arrayrulecolor{gray}
\newcount\blockcount
\def\blocks#1{\blockcount=0 \doblocks#1@}
\def\doblocks#1{%
\ifx#1@%
\hskip.5\extrarowheight{}%
\else%
\advance\blockcount by1 %
\ifx#1x%
\doblock{block\the\blockcount}%
\else%
\doblock{white}%
\fi%
\expandafter\doblocks%
\fi}
\def\doblock#1{%
\textcolor{#1}{\raisebox{-.5\extrarowheight}{\vrule width 3ex height3ex depth0.0ex}}%
}
\colorlet{block1}{green!60!yellow!80!black}
\colorlet{block2}{blue!80!red!70!black}
\colorlet{block3}{orange}
\colorlet{block4}{blue!50!cyan}
\colorlet{block5}{orange!50!red}
\colorlet{block6}{yellow}
\colorlet{block7}{black!30}
\begin{document}
\begin{tabular}{|p{2in}l@{}|}\hline
Learning unit 1 & \blocks{..xxx..} \\\hline
Learning unit 2 & \blocks{..xxx..} \\\hline
Learning unit 3 & \blocks{xxx.xx.} \\\hline
Learning unit 4 & \blocks{..xxxx.} \\\hline
Learning unit 5 & \blocks{..xxx..} \\\hline
Learning unit 6 & \blocks{..xxxxx} \\\hline
Learning unit 7 & \blocks{xxxxxxx} \\\hline
Learning unit 8 & \blocks{xx..xx.} \\\hline
Learning unit 9 & \blocks{xx..xxx} \\\hline
Learning unit 10 & \blocks{xx..xx.} \\\hline
Learning unit 11 & \blocks{x...x..} \\\hline
Learning unit 12 & \blocks{...xxxx} \\\hline
Learning unit 13 & \blocks{...x.xx} \\\hline
Learning unit 14 & \blocks{x..xx..} \\\hline
Learning unit 15 & \blocks{x..xx..} \\\hline
\end{tabular}
\end{document}
答案3
可以用一个简单的表格来完成:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{libertine}
\usepackage[table,dvipsnames,X11names]{xcolor}
\def\myBox{\rule[-1.5ex]{5ex}{5ex}}
\def\W{\phantom{\myBox}}
\def\G{\textcolor{green}{\myBox}}
\def\V{\textcolor{blue!100!red!80}{\myBox}}
\def\B{\textcolor{blue}{\myBox}}
\def\O{\textcolor{orange}{\myBox}}
\def\R{\textcolor{red}{\myBox}}
\def\Y{\textcolor{yellow}{\myBox}}
\def\g{\textcolor{gray}{\myBox}}
\begin{document}
{\fboxsep=0pt\fboxrule=1pt
\fbox{%
\arrayrulecolor{black!20}\arrayrulewidth=0.8pt%
\begin{tabular}{ >{Learning unit }p{0.4\linewidth}l}
1 & \W\W\O\B\R\W\W \\\hline
2 & \W\W\O\B\R\W\W \\\hline
3 & \G\V\O\W\R\Y\W \\\hline
4 & \W\W\O\W\R\Y\W \\\hline
5 & \W\W\O\W\R\W\W \\\hline
6 & \G\V\O\W\R\Y\g \\\hline
7 & ...
\end{tabular}}
\end{document}
如果你喜欢玩活跃角色,你可以简化表格:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{libertine}
\usepackage[table,dvipsnames,X11names]{xcolor}
\def\mybox{\rule[-1.5ex]{5ex}{5ex}}
\begingroup
\makeatletter
\catcode`\W=\active \catcode`\G=\active
\catcode`\V=\active \catcode`\B=\active
\catcode`\O=\active \catcode`\R=\active
\catcode`\Y=\active \catcode`\K=\active
\@firstofone{\endgroup
\newenvironment{makeActive}{%
\catcode`\W=\active \catcode`\G=\active
\catcode`\V=\active \catcode`\B=\active
\catcode`\O=\active \catcode`\R=\active
\catcode`\Y=\active \catcode`\K=\active
\def W{\phantom{\mybox}}% white box
\def G{\textcolor{green}{\mybox}}%
\def V{\textcolor{blue!100!red!80}{\mybox}}%
\def B{\textcolor{blue}{\mybox}}%
\def O{\textcolor{orange}{\mybox}}%
\def R{\textcolor{red}{\mybox}}%
\def Y{\textcolor{yellow}{\mybox}}%
\def K{\textcolor{gray}{\mybox}}%
}{}%
}
\makeatother
\begin{document}
\begin{makeActive}
\fboxsep=0pt\fboxrule=1pt
\fbox{\arrayrulecolor{black!20}\arrayrulewidth=0.8pt%
\begin{tabular}{ >{Learning unit }p{0.4\linewidth}l}
1 & WWOBRWW \\\hline
2 & WWOBRWW \\\hline
3 & GVOWRYW \\\hline
4 & WWOWRYW \\\hline
5 & WWOWRWW \\\hline
6 & GVOWRYK
%7 & ...
\end{tabular}}
\end{makeActive}
\end{document}