最近,我必须处理一些需要在 LaTeX 中绘制如下表格的问题。我以前考虑过在单元格内画对角线,但对于这个问题来说这不是一个好选择,因为文本不在对角线上。有人在 LaTeX 中创建过类似的东西吗,你能给我一些解决方案吗
答案1
更新:参见改进版本自己的答案。
这是该表的 MWE。
\documentclass{minimal}
\usepackage{tikz}
\newlength{\csize}
\newlength{\cwidth}
\newlength{\clabel}
\newcommand{\cell}[1]% centers text in cell
{node[above right,minimum width=\csize, minimum height=\csize]{#1}}
\begin{document}
\setlength{\csize}{1.5\baselineskip}% cell size (note: \baselineskip depends on current font)
\settowidth{\clabel}{simple label}% label width
\addtolength{\clabel}{1em}
\setlength{\cwidth}{2\csize}% total width (2 cells)
\addtolength{\cwidth}{\clabel}
\begin{tikzpicture}
\path (0,0) node[above right,fill=yellow,minimum width=\cwidth, minimum height=\csize]{};
\draw% horizontal lines
(0,0) -- (\cwidth,0)
(0,\csize) -- (\cwidth,\csize)
(0,2\csize) -- (\cwidth,2\csize);
\path% labels
(0,0) node[above right]{left}
(\clabel,0) node[above left]{right}
(0,\csize) node[above right]{simple label};
\begin{scope}[xshift=\clabel]
\draw% vertical lines
(0,0) -- (0,2\csize)
(\csize,0) -- (\csize,2\csize)
(2\csize,0) -- (2\csize,2\csize);
\path% fill cells
(0,\csize) \cell{1}
(\csize,\csize) \cell{2}
(0,0) \cell{3}
(\csize,0) \cell{4};
\end{scope}
\begin{scope}[xshift=\clabel, yshift=2\csize]
\draw% diagonal lines
(0,0) -- ++(45:\clabel)
(\csize,0) -- ++(45:\clabel)
(2\csize,0) -- ++(45:\clabel);
\path% diagonal labels
(\csize,0) node[above right,rotate=45]{first}
(2\csize,0) node[above right,rotate=45]{second};
\end{scope}
\end{tikzpicture}
\end{document}
答案2
不包含以下内容的表格复制品tikz
:
\documentclass{article}
\usepackage[hmargin=20mm]{geometry}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{colortbl}
\usepackage{array}
\usepackage{pifont}
\begin{document}
\begingroup
\renewcommand*{\arraystretch}{1.5}%
\definecolor{tabred}{RGB}{230,36,0}%
\definecolor{tabgreen}{RGB}{0,116,21}%
\definecolor{taborange}{RGB}{255,124,0}%
\definecolor{tabbrown}{RGB}{171,70,0}%
\definecolor{tabyellow}{RGB}{255,253,169}%
\newcommand*{\redtriangle}{\textcolor{tabred}{\ding{115}}}%
\newcommand*{\greenbullet}{\textcolor{tabgreen}{\ding{108}}}%
\newcommand*{\orangecirc}{\textcolor{taborange}{\ding{109}}}%
\newcommand*{\headformat}[1]{{\small#1}}%
\newcommand*{\vcorr}{%
\vadjust{\vspace{-\dp\csname @arstrutbox\endcsname}}%
\global\let\vcorr\relax
}%
\newcommand*{\HeadAux}[1]{%
\multicolumn{1}{@{}r@{}}{%
\vcorr
\sbox0{\headformat{\strut #1}}%
\sbox2{\headformat{Complex Data Movement}}%
\sbox4{\kern\tabcolsep\redtriangle\kern\tabcolsep}%
\sbox6{\rotatebox{45}{\rule{0pt}{\dimexpr\ht0+\dp0\relax}}}%
\sbox0{\raisebox{.5\dimexpr\dp0-\ht0\relax}[0pt][0pt]{\unhcopy0}}%
\kern.75\wd4 %
\rlap{%
\raisebox{.25\wd4}{\rotatebox{45}{\unhcopy0}}%
}%
\kern.25\wd4 %
\ifx\HeadLine Y%
\dimen0=\dimexpr\wd2+.5\wd4\relax
\rlap{\rotatebox{45}{\hbox{\vrule width\dimen0 height .4pt}}}%
\fi
}%
}%
\newcommand*{\head}[1]{\HeadAux{\global\let\HeadLine=Y#1}}%
\newcommand*{\headNoLine}[1]{\HeadAux{\global\let\HeadLine=N#1}}%
\noindent
\begin{tabular}{%
>{\bfseries}lc|>{\quad}c
*{4}{c|}c>{\quad}c
*{4}{c|}c>{\quad}c
*{2}{c|}c%
}%
&
\head{Data Dimensions} &
&
\head{Single Input/Output} &
\head{Statc Rates} &
\head{Hierachical Memory} &
\head{Complex Data Movement} &
\headNoLine{Single Kernel Execution} &
&
\head{Application graph} &
\head{Implicit memory access} &
\head{Theoretical Framework} &
\head{Out-of-band control} &
\headNoLine{Real-time constraints} &
&
\head{Data Parallelism} &
\head{Pipeline Parallelism} &
\headNoLine{Task Parallelism}
\\
\sbox0{S}%
\rule{0pt}{\dimexpr\ht0 + 2ex\relax}%
StreamIt & \textcolor{tabred}{\bfseries 1} &&
\redtriangle & \redtriangle & & & &&
\greenbullet & \greenbullet & \orangecirc & \greenbullet & &&
\greenbullet & \greenbullet & \greenbullet
\\\hline
StreamC/KernelC & \textcolor{tabred}{\bfseries 1} &&
& & & & \redtriangle &&
\greenbullet & \greenbullet & & \greenbullet & &&
\greenbullet & &
\\\hline
SDF & \textcolor{tabred}{\bfseries 1} &&
& \redtriangle & & & &&
\greenbullet & \greenbullet & \greenbullet & & &&
& \greenbullet & \greenbullet
\\\hline
MDSDF & \textcolor{tabgreen}{\bfseries\itshape n} &&
& \redtriangle & & \redtriangle & &&
\greenbullet & \greenbullet & \greenbullet & & &&
\greenbullet & \greenbullet & \greenbullet
\\\hline
ArrayOL & \textcolor{tabgreen}{\bfseries\itshape n} &&
& \redtriangle & & \redtriangle & &&
\greenbullet & \greenbullet & \orangecirc & & &&
\greenbullet & \greenbullet & \greenbullet
\\\hline
Brook & \textcolor{tabgreen}{\bfseries\itshape n} &&
& & & & \redtriangle &&
& \greenbullet & & & &&
\greenbullet & &
\\\hline
Sequoia & \textcolor{tabgreen}{\bfseries\itshape n} &&
& & \redtriangle & \redtriangle & \redtriangle &&
& & & & &&
\greenbullet & &
\\\hline
\rowcolor{tabyellow}%
Block-parallel & \textcolor{tabbrown}{\bfseries 2} &&
& \redtriangle & & & &&
\greenbullet & \greenbullet & \orangecirc & \greenbullet &
\greenbullet &&
\greenbullet & \greenbullet & \greenbullet
\\[.5ex]
\multicolumn{3}{c}{} &
\multicolumn{5}{c}{\bfseries Limitations} &&
\multicolumn{5}{c}{\bfseries Features} &&
\multicolumn{3}{c}{\bfseries Parallelism}
\\
\end{tabular}%
\kern19.5mm % manually with the help of the next \vrule
% \vrule height 50mm
\endgroup
\end{document}
评论:
- 颜色值取自图像。
- 这些符号来自符号字体 Zapf Dingbats。
\ding
包pifont
可以访问这些符号。 - 最后一张表格的背景颜色由
\rowcolor
包完成colortbl
。 - 旋转是通过
\rotatebox
(graphics
)graphicx
包完成的。 - 头线行的深度为空,因此垂直线会缺失。通过将头线向下移动
\vcorr
直到旋转的线碰到垂直列线,可以解决这个问题。 - 表格末尾的值
\kern
是借助以下规则手动设置的。空白区域通知\kern
LaTeX,由于旋转的标题重叠,表格比它认为的要大。
答案3
这是改进的版本(通用范围)。
\documentclass{minimal}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{calc}
% create symbols
\newcommand{\reddot}{\tikz{\fill[red] (0,0) circle (4pt);}}
\newcommand{\greentriangle}{\tikz{\fill[green] (0,0) -- (4.62pt,8pt) -- (9.23pt,0) -- cycle;}}
\newcommand{\bigx}{\tikz{\draw[black,thick] (0,0) -- (7pt,7pt) (0,7pt) -- (7pt,0);}}
\newlength{\csize}
\newlength{\clabel}
\newlength{\cwidth}
\newcommand{\cell}[1]% centers text in cell
{node[above right,minimum width=\csize, minimum height=\csize]{#1}}
\begin{document}
test \reddot \greentriangle \bigx \checkmark $\bigstar$
\setlength{\csize}{1.5\baselineskip}% cell size (note: \baselineskip depends on current font)
\addtolength{\csize}{.6666em}
\settowidth{\clabel}{longest label}% label width
\addtolength{\clabel}{.6666em}
\setlength{\cwidth}{2\csize}% total width
\addtolength{\cwidth}{\clabel}
\begin{tikzpicture}
\path (-\clabel,-2\csize) node[above right,fill=yellow,minimum width=\cwidth, minimum height=\csize]{};
\draw% horizontal lines
(-\clabel,0) -- (2\csize,0)
(-\clabel,-\csize) -- (2\csize,-\csize)
(-\clabel,-2\csize) -- (2\csize,-2\csize);
\path% labels
(-\clabel,-2\csize) node[above right]{left}
(0,-2\csize) node[above left]{right}
(-0.5\clabel,-\csize) node[above]{center};
\draw% vertical lines
(0,0) -- (0,-2\csize)
(\csize,0) -- (\csize,-2\csize)
(2\csize,0) -- (2\csize,-2\csize);
\path% fill cells
(0,-\csize) \cell{1}
(\csize,-\csize) \cell{2}
(0,-2\csize) \cell{3}
(\csize,-2\csize) \cell{4};
\draw% diagonal lines
(0,0) -- ++(45:\clabel)
(\csize,0) -- ++(45:\clabel)
(2\csize,0) -- ++(45:\clabel);
\path% diagonal labels
(\csize,0) node[above right,rotate=45]{first}
(2\csize,0) node[above right,rotate=45]{second};
\end{tikzpicture}
\end{document}