如何为LaTeX矩阵添加标题?规避表格形式及报错问题
解决LaTeX矩阵添加标题的问题
错误原因分析
\caption outside float报错:\caption命令仅支持在浮动环境(如figure、table)中使用,equation不属于浮动环境,直接在其中调用\caption会触发错误。undefined control sequence(\captionof)报错:\captionof命令来自caption宏包,你未导入该包,导致命令未定义。
可行解决方案
以下两种方案均无需将矩阵转为表格,保留equation环境的数学排版效果:
方案1:用浮动环境包裹equation(推荐,自动编号)
通过figure浮动环境包裹equation,既能保留公式的自动编号,又能使用\caption添加标题:
\documentclass{article} \usepackage{amsmath} \usepackage{caption} % 必须加载的依赖包 \begin{document} \begin{figure}[htbp] \centering \begin{equation} \begin{bmatrix} \cos\theta_i & -\sin\theta_i \cdot \cos\alpha_i & \sin\theta_i \cdot \cos\alpha_i & l_n \cdot \cos\theta_i \\ \sin\theta_i & \cos\theta_i \cdot \cos\alpha_i & -\cos\theta_i \cdot \sin\alpha_i & l_n \cdot \sin\theta_i \\ 0 & \sin\alpha_i & \cos\alpha_i & d_i \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{equation} \caption{HTM of Manipulator} % 自动生成标题编号,如需自定义可改用\caption*{Matrix 3.1: HTM of Manipulator} \end{figure} \end{document}
方案2:用minipage+\captionof(无浮动,手动控制位置)
如果不想使用浮动环境,可将equation放在minipage中,搭配\captionof添加标题:
\documentclass{article} \usepackage{amsmath} \usepackage{caption} % 必须加载的依赖包 \begin{document} \begin{minipage}{\linewidth} \centering \begin{equation} \begin{bmatrix} \cos\theta_i & -\sin\theta_i \cdot \cos\alpha_i & \sin\theta_i \cdot \cos\alpha_i & l_n \cdot \cos\theta_i \\ \sin\theta_i & \cos\theta_i \cdot \cos\alpha_i & -\cos\theta_i \cdot \sin\alpha_i & l_n \cdot \sin\theta_i \\ 0 & \sin\alpha_i & \cos\alpha_i & d_i \\ 0 & 0 & 0 & 1 \end{bmatrix} \end{equation} \captionof{figure}{Matrix 3.1: HTM of Manipulator} % 可完全自定义标题内容与编号 \end{minipage} \end{document}
额外修正提示
你的原代码中有几处小问题:
ln应改为l_n(LaTeX中数学下标需用_实现);sin应改为\sin(LaTeX内置数学函数命令,需加反斜杠以正确排版);- 第三行的
\sin\theta_i \cdot \cos\alpha_i疑似笔误,修正为\sin\alpha_i(符合DH参数矩阵的标准形式)。
内容的提问来源于stack exchange,提问作者SohaibAJ
相关产品推荐
相关产品推荐

