cleveref 带子标题,使用 \crefmultiformat 和 \crefstripprefix

cleveref 带子标题,使用 \crefmultiformat 和 \crefstripprefix

我正在尝试实施这个解决方案: https://tex.stackexchange.com/a/418261/194338 或者这个解决方案: https://tex.stackexchange.com/a/416838/194338

但在我的例子中,两种方法都失败了。下面是一个最小示例:

\documentclass{article}
\usepackage{subcaption}
\usepackage{cleveref}
\crefrangelabelformat{figure}{#3#1#4 to #5\crefstripprefix{#1}{#2}#6}

\crefmultiformat{figure}%
  {\edef\mycrefprefixinfo{#1}figs.~#2#1#3}%
  { and~#2\crefstripprefix{\mycrefprefixinfo}{#1}#3}%  <- cannot access \mycrefprefixinfo when there is a range created
  {, #2\crefstripprefix{\mycrefprefixinfo}{#1}#3}%
  {, and~#2\crefstripprefix{\mycrefprefixinfo}{#1}#3}

 
\begin{document}

\cref{fig:test:1,fig:test:2,fig:test:3} % works: figs. 1a to c
\cref{fig:test:1,fig:test:3} % works: figs. 1a and c
\cref{fig:test:1,fig:test:3,fig:test:4} % works: figs. 1a, c, and d
\cref{fig:test:1,fig:test:2,fig:test:3,fig:test:5} % fails, should be figs. 1a to c and e

\begin{figure}
\centering
{\phantomsubcaption\label{fig:test:1}}%
{\phantomsubcaption\label{fig:test:2}}%
{\phantomsubcaption\label{fig:test:3}}%
{\phantomsubcaption\label{fig:test:4}}%
{\phantomsubcaption\label{fig:test:5}}%
\caption{General caption}
\end{figure}


\end{document}

基本上,当它创建一个范围时,它似乎会失败。从我的测试来看,当有一个范围时,该参数\mycrefprefixinfo在 \crefmultiformat 的第三个参数中不再可访问

答案1

因此根据@AndrewSwann 的评论和@AndrewSwann 的更新答案:为子图引用对定制 cleveref:[1a 和 b] 而不是 [1a 和 1b]

需要进行两处修改:更改\edef为并添加里面\xdef变量的定义:\mycrefprefixinfo\crefrangelabelformat

\crefrangelabelformat{figure}%
  {\xdef\mycrefprefixinfo{#1}#3#1#4 to #5\crefstripprefix{#1}{#2}#6}

\crefmultiformat{figure}%
  {\xdef\mycrefprefixinfo{#1}figs.~#2#1#3}%
  { and~#2\crefstripprefix{\mycrefprefixinfo}{#1}#3}%
  {, #2\crefstripprefix{\mycrefprefixinfo}{#1}#3}%
  {, and~#2\crefstripprefix{\mycrefprefixinfo}{#1}#3}

根据用例,可能还需要重新定义\crefrangemultiformat

如果仅调用,而不调用,则对于上述代码,需要\mycrefprefixinfo在里面添加变量的定义,这会将 的定义放在全局空间中。\crefrangelabelformat\cref{fig:test:1,fig:test:2,fig:test:3,fig:test:5}\cref{fig:test:1,fig:test:3}\mycrefprefixinfo

相关内容