![LaTeX 中的混淆矩阵](https://linux22.com/image/370525/LaTeX%20%E4%B8%AD%E7%9A%84%E6%B7%B7%E6%B7%86%E7%9F%A9%E9%98%B5.png)
我使用了“仅使用 LaTeX 代码的混淆矩阵“
我增加了类别数量,直到混淆矩阵达到 9x9,一切都运行良好,但对于 10 或更高的数字,它会出现一些错误。我上传了 10 列的示例:
\documentclass{article}
\usepackage{graphicx}
\newcommand\MyBox[1]{%
\fbox{\parbox[c][.7cm][c]{.7cm}{\centering #1}}%
}
\newcommand\MyVBox[1]{%
\parbox[c][.7cm][c]{1cm}{\centering\bfseries #1}%
}
\newcommand\MyHBox[2][\dimexpr.7cm+2\fboxsep\relax]{%
\parbox[c][1cm][c]{#1}{\centering\bfseries #2}%
}
\newcommand\MyTBox[10]{%
\MyVBox{#1}\MyBox{#2}\hspace*{-\fboxrule}\MyBox{#3}\hspace*{-\fboxrule}%
\MyBox{#4}\hspace*{-\fboxrule}%
\MyBox{#5}\hspace*{-\fboxrule}%
\MyBox{#6}\hspace*{-\fboxrule}%
\MyBox{#7}\hspace*{-\fboxrule}%
\MyBox{#8}\hspace*{-\fboxrule}%
\MyBox{#9}\hspace*{-\fboxrule}%
\MyBox{#10}\par\vspace{-\fboxrule}
}
\begin{document}
{
\offinterlineskip
\raisebox{-5cm}[0pt][0pt]{\rotatebox[origin=c]{90}{\parbox[c][0pt][c]{1cm}{\textbf{Source2}\\[20pt]}}}\par
\hspace*{1cm}\MyHBox[\dimexpr5.1cm+6\fboxsep\relax]{Source1}\par
\hspace*{1cm}\MyHBox{A}\MyHBox{B}\MyHBox{C}
\MyHBox{D}\MyHBox{E}\MyHBox{F}\par
\MyTBox{A}{1}{2}{3}{4}{5}{6}{7}{8}{9}
\MyTBox{B}{2}{3}{4}{5}{6}{7}{8}{9}{10}
\MyTBox{C}{3}{4}{5}{6}{7}{8}{9}{10}{11}
\MyTBox{D}{4}{5}{6}{7}{8}{9}{10}{11}{12}
\MyTBox{E}{5}{6}{7}{8}{9}{10}{11}{12}{13}
\MyTBox{F}{6}{7}{8}{9}{10}{11}{12}{13}{14}
}
\end{document}
答案1
与其为宏使用 10 个参数,不如使用逗号分隔的列表,然后使用包docsvlist
中的类似方法对其进行处理etoolbox
。通过这个小修改,您的代码将生成:
完整代码如下:
\documentclass{article}
\usepackage{graphicx}
\newcommand\MyBox[1]{%
\fbox{\parbox[c][.7cm][c]{.7cm}{\centering #1}}%
}
\newcommand\MyVBox[1]{%
\parbox[c][.7cm][c]{1cm}{\centering\bfseries #1}%
}
\newcommand\MyHBox[2][\dimexpr.7cm+2\fboxsep\relax]{%
\parbox[c][1cm][c]{#1}{\centering\bfseries #2}%
}
\usepackage{etoolbox}
\newcommand\MyTBox[3]{%
\MyVBox{#1}
\renewcommand*\do[1]{\MyBox{##1}\hspace*{-\fboxrule}}
\docsvlist{#2}
\MyBox{#3}\par\vspace{-\fboxrule}
}
\begin{document}
{
\offinterlineskip
\raisebox{-5cm}[0pt][0pt]{\rotatebox[origin=c]{90}{\parbox[c][0pt][c]{1cm}{\textbf{Source2}\\[20pt]}}}\par
\hspace*{1cm}\MyHBox[\dimexpr5.1cm+6\fboxsep\relax]{Source1}\par
\hspace*{1cm}\MyHBox{A}\MyHBox{B}\MyHBox{C}
\MyHBox{D}\MyHBox{E}\MyHBox{F}\par
\MyTBox{A}{1, 2, 3, 4, 5, 6, 7, 8}{9}
\MyTBox{B}{2, 3, 4, 5, 6, 7, 8, 9}{10}
\MyTBox{C}{3, 4, 5, 6, 7, 8, 9, 10}{11}
\MyTBox{D}{4, 5, 6, 7, 8, 9, 10, 11}{12}
\MyTBox{E}{5, 6, 7, 8, 9, 10, 11, 12}{13}
\MyTBox{F}{6, 7, 8, 9, 10, 11, 12, 13}{14}
}
\end{document}
我设置了\MyTBox
三个参数,因为分别对页眉、正文和页脚进行了“特殊”处理。话虽如此,我可能会使用类似 之类的方法以不同的方式执行此操作tikz
,尽管这实际上取决于您实际在这些宏中放入的内容:)——但我也喜欢您的宏!
答案2
谢谢,我按照@daleif 的评论并成功做到了。
\newcommand\MyTBox[9]{%
%....
\MyTBoxcontinued
}
\newcommand\MyTBoxcontinued[2]{%
%two more arguments
}
下面,我上传了代码和生成的图像。
\documentclass{article}
\usepackage{graphicx}
\usepackage{tipa}
\usepackage[normalem]{ulem}
\newcommand\MyBox[1]{%
\fbox{\parbox[c][.7cm][c]{.7cm}{\centering #1}}%
}
\newcommand\MyVBox[1]{%
\parbox[c][.7cm][c]{1cm}{\centering\bfseries #1}%
}
\newcommand\MyHBox[2][\dimexpr.7cm+2\fboxsep\relax]{%
\parbox[c][1cm][c]{#1}{\centering\bfseries #2}%
}
\newcommand\MyTBox[9]{%
\MyVBox{#1}\MyBox{#2}\hspace*{-\fboxrule}\MyBox{#3}\hspace*{-\fboxrule}%
\MyBox{#4}\hspace*{-\fboxrule}%
\MyBox{#5}\hspace*{-\fboxrule}%
\MyBox{#6}\hspace*{-\fboxrule}%
\MyBox{#7}\hspace*{-\fboxrule}%
\MyBox{#8}\hspace*{-\fboxrule}%
\MyBox{#9}\hspace*{-\fboxrule}%
\MyTBoxcontinued
}
\newcommand\MyTBoxcontinued[2]{%
\MyBox{#1}\hspace*{-\fboxrule}%
\MyBox{#2}\par\vspace{-\fboxrule}
}
\begin{document}
{
\offinterlineskip
\hspace*{1cm}\MyHBox{/\textipa{a}/}\MyHBox{/\textipa{E}/}\MyHBox{\textipa{/I/}}
\MyHBox{/\reflectbox{\textipa{c}}/}\MyHBox{/\textipa{U}/}\MyHBox{/\textipa{a:}/}
\MyHBox{/\textipa{e}/}\MyHBox{/\textipa{i}/}\MyHBox{/\textipa{o}/}\MyHBox{/\textipa{u}/}\par
\MyTBox{/\textipa{a}/}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}
\MyTBox{/\textipa{E}/}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}
\MyTBox{/\textipa{I}/}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}
\MyTBox{/\reflectbox{\textipa{c}}/}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}
\MyTBox{/\textipa{U}/}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}
\MyTBox{/\textipa{a:}/}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}
\MyTBox{/\textipa{e}/}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}
\MyTBox{/\textipa{o}/}{8}{9}{10}{11}{12}{13}{14}{15}{16}{17}
\MyTBox{/\textipa{u}/}{9}{10}{11}{12}{13}{14}{15}{16}{17}{18}
}
\end{document}