我怎样才能画一条对角线,一边写着评级,另一边写着书籍?
这是@Marmot 制作的一个例子:
\documentclass{article}
\usepackage{amsmath,blkarray}
\begin{document}
\[
\begin{blockarray}{lccc}
\text{Ratings} & \text{book}_1 & \text{book}_2 & \text{book}_3 \\
\begin{block}{l(ccc)}
\text{user}_1 & - & 1 & - \smash[b]{\vphantom{\Big|}}\\
\text{user}_2 & - & 5 & - \\
\text{user}_3 & 4 & - & - \\
\text{user}_4 & - & - & 2\smash[t]{\vphantom{\Big|}}\\
\end{block}
\end{blockarray}
\]
\end{document}
期望输出:
答案1
例如,您可以使用slashbox
包来实现所需的输出。在下面的 MWE 中,我还包含了一个可能也很有趣的替代方案:
\documentclass{article}
\usepackage{amsmath,blkarray}
%%%%% Only for the second example %%%%%
\usepackage{slashbox}
\usepackage{array,multirow,graphicx}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\[
\begin{blockarray}{lccc}
\text{\backslashbox{Ratings}{Books}} & \text{book}_1 & \text{book}_2 & \text{book}_3 \\
\begin{block}{l(ccc)}
\text{user}_1 & - & 1 & - \smash[b]{\vphantom{\Big|}}\\
\text{user}_2 & - & 5 & - \\
\text{user}_3 & 4 & - & - \\
\text{user}_4 & - & - & 2\smash[t]{\vphantom{\Big|}}\\
\end{block}
\end{blockarray}
\]
\[
\begin{blockarray}{llccc}
& & \multicolumn{3}{c}{Books} \\
& & \text{book}_1 & \text{book}_2 & \text{book}_3 \\
\begin{block}{ll(ccc)}
\parbox[t]{2mm}{\multirow{4}{*}{\rotatebox[origin=c]{90}{Rating}}} & \text{user}_1 & - & 1 & - \smash[b]{\vphantom{\Big|}}\\
& \text{user}_2 & - & 5 & - \\
& \text{user}_3 & 4 & - & - \\
& \text{user}_4 & - & - & 2\smash[t]{\vphantom{\Big|}}\\
\end{block}
\end{blockarray}
\]
\end{document}