在 \cref 语句或类似语句中显示新命令定义的参数

在 \cref 语句或类似语句中显示新命令定义的参数

我正在寻找一种方法来在 \cref 语句中引用多值对象中的成员(\newcommand定义中的参数),例如这里

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{tabularx}

\floatstyle{ruled}
\newfloat{myfloat}{H}{flo}%[section]
\floatname{myfloat}{My Float}

\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[nameinlink]{cleveref}
\crefname{myfloat}{My Float}{My Floats}

\newcommand{\multivaluefloat}[4]{%
  \begin{myfloat}%
    \caption{#2}%
    \label{#1}
    #3

    #4
  \end{myfloat}%
}

\begin{document}

\listof{myfloat}{}

\multivaluefloat{label-a}{This is a caption}{A portion of text.}{Another portion of text.}

In some text we reference \cref{label-a}

\begin{tabularx}{\linewidth}{XXX}
  % Searching for a command referencing 'label-a' to print the equivalent of
  My Float 1 & This is a caption & A portion of text.
\end{tabularx}

\end{document}

答案1

您必须将第三个和第四个参数保存\multivaluefloat在辅助宏中。一种方法是

\documentclass{article}
\usepackage{lipsum}
\usepackage{float}
\usepackage{tabularx}

\floatstyle{ruled}
\newfloat{myfloat}{H}{flo}%[section]
\floatname{myfloat}{My Float}

\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[nameinlink]{cleveref}
\crefname{myfloat}{My Float}{My Floats}

\newcommand{\multivaluefloat}[4]{%
  \expandafter\def\csname#1@first\endcsname{#3}%  <-- added this
  \expandafter\def\csname#1@second\endcsname{#4}% <-- added this
  \begin{myfloat}%
    \caption{#2}%
    \label{#1}
    #3\par
    #4
  \end{myfloat}%
}

% added following line
\newcommand*{\printfloat}[1]{\cref{#1} & \csname#1@first\endcsname & \csname#1@second\endcsname }

\begin{document}

\listof{myfloat}{}

\lipsum[66]

\multivaluefloat{label-a}{This is a caption}{A portion of text.}{Another portion of text.}

In some text we reference \cref{label-a}. \lipsum[66]

\multivaluefloat{another}{This is another caption}{Some stuff here.}{Some stuff there.}

\lipsum[66]

\bigskip
\noindent
\begin{tabularx}{\linewidth}{XXX}
  \printfloat{label-a}\\
  \printfloat{another}
\end{tabularx}

\end{document}

在此处输入图片描述

当然,现在\printfloat只是按照你的要求打印表格行。当然,可以有更灵活的方法来分别访问各个部分,但我不会深入研究这一点,因为我不知道你是什么实际上将要做...

相关内容