LaTeX 标题中使用 m 命令与 hyperref 的冲突

问题

当使用 hyperref 宏包时,在标题中使用 m 为数学符号加粗会出现错误

documentclass{article}
usepackage{bm}
usepackage{hyperref}

egin{document}

section{$m{x^2}$}

end{document}

报错信息为

ERROR: TeX capacity exceeded, sorry [input stack size=5000].

原因是,使用 hyperref 创建的书签信息中不能含有特殊格式。

解决方法

有如下的几种解决方法

  1. 使用标题的可选项
    section[$x^2$]{$m{x^2}$}
  1. 使用 hyperref exorpdfstring 命令
    section{	exorpdfstring{$m{x^2}$}{$x^2$}}
  1. 使用 hyperrefpdfstringdefDisableCommands 命令抑制命令作用在PDF字符串上
    documentclass{article}
    usepackage{bm}
    usepackage{hyperref}
    pdfstringdefDisableCommands{%
    
enewcommand*{m}[1]{#1}%
    % any other necessary redefinitions 
    }

    egin{document}
    section{$m{x^2}$}
    
    end{document}

最后一种方法使用起来比较方便,因为不需要改变正文。


参考信息

原文地址:https://www.cnblogs.com/wenbosheng/p/6169065.html