在标题 LaTex 中使用罗马数字时出错

Error when using roman numerals in section heading LaTex

我想用罗马数字来枚举我的 Latex 文档中的某些部分。

我用这个:\MakeUppercase{\romannumeral 2} 来创建数字。这在普通文本中工作正常,但如果我尝试在 sectionsubsection 命令中使用它,我会得到一个“undefined control sequence”错误,即使它似乎在节标题中正确显示了数字。

显然问题出在 \MakeUppercase 命令上,因为 \romannumeral 2 命令没有给出错误。

我也试过定义一个\newcommand有同样的效果。

有人知道为什么会这样吗?

编辑:在创建了一个最小的可重现示例后,我发现问题与 hyperref 包有关。

\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}

\usepackage{hyperref}

\newcommand{\RomanNumeralCaps}[1]{\MakeUppercase{\romannumeral #1}}

\begin{document}

\chapter{Some chapter}
\section{Some section}
\subsection{Experiment \MakeUppercase{\romannumeral 1}}
\MakeUppercase{\romannumeral 1}
\RomanNumeralCaps{2}

\end{document}

您可以为 pdf 书签提供替代字符串:

\documentclass[12pt,a4paper]{report}
\usepackage[utf8]{inputenc}

\usepackage{romannum}% for approach #1 and #2
\usepackage{biblatex}% for approach #3
\usepackage{hyperref}


\begin{document}

\chapter{Some chapter}
\section{Some section}
\subsection{Experiment \texorpdfstring{\Romannum{1}}{I}}

or

\subsection[Experiment II]{Experiment \Romannum{2}}

or

\subsection{Experiment \RN{3}}

However with this approach the III will be replaced by 3 in the pdf bookmarks

\end{document}