引用格式化的文本

引用格式化的文本

我正在寻找一种简单的方法来引用文档中的文本。类似以下内容:

\documentclass{article}

\def\labeltext[#1]#2{#2}

\begin{document}
Refer forward to (\ref{text:first}) and to (\ref{text:second}).

This is my \labeltext[text:first]{\textbf{First} item}.
This is my \labeltext[text:second]{\textit{Second} item}.

Refer back to (\ref{text:first}) and to (\ref{text:second}).

\end{document}

我想要输出

请参阅(第一的项目)和(第二物品)。

这是我的第一的项目。这是我的第二物品。

请参阅 (第一的项目)和(第二物品)。

有办法实现这个吗?\ref如果可能的话,我更愿意使用该命令(希望其他软件包也喜欢cleverefhyperref能很好地使用它),而不是创建新的自定义引用命令。

编辑:我发现了这个解决方案它适用于纯文本,但不适用于粗体/斜体文本。

答案1

您可以使用该包参考值用于实现您自己的引用命令。

通过以下示例,重新实现了一些内容,以便\long可引用的文本片段也可以由几个小段落组成。

但是可引用的文本片段会作为一行 .tex 输入写入 .aux 文件。因此可引用文本的数量受到 TeX 可从一行 .tex 输入中读取的文本量的限制。这又取决于平台。

\documentclass{article}
\usepackage[user]{zref}

\makeatletter
% Implement some zref-infrastructure in terms of \long :
\@ifdefinable\zref@setcurrentlong{%
  \protected\long\def\zref@setcurrentlong#1#2{%
     \zref@propexists{#1}{\expandafter\def\csname Z@C@#1\endcsname{#2}}%
  }%
}%
\protected\long\def\zref@newlabel{%
  \@newl@bellong{Z@R}%
}%
\newcommand\@newl@bellong[3]{%
  \ifx\@newl@bel\@testdef\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {%
    \def\reserved@a{#3}%
    \expandafter\ifx\csname #1@#2\endcsname\reserved@a\else\@tempswatrue\fi
  }{%
    {%
      \@ifundefined{#1@#2}\relax{%
        \gdef\@multiplelabels{\@latex@warning@no@line{There were multiply-defined labels}}%
        \@latex@warning@no@line {Label `#2' multiply defined}%
      }\global\@namedef{#1@#2}{#3}%
    }%
  }%
}%
\def\ZREF@@@newprop#1{%
  \long\expandafter\gdef\csname Z@E@\ZREF@P\endcsname##1#1##2##3\ZREF@nil{##2}%
}%
%------------------------------------------------------------------------------------------
\zref@newprop{MyCurrentText}{}%
\newcommand\MyTextref[1]{\zref[{MyCurrentText}]{#1}}%
\newcommand\MyLabeltext[2]{%
  \zref@setcurrentlong{MyCurrentText}{#2}%
  \zref@labelbyprops{#1}{MyCurrentText}%
  \MyTextref{#1}%
}%
\makeatother

\begin{document}
Refer forward to (\MyTextref{text:first}) and to (\MyTextref{text:second}).

This is my \MyLabeltext{text:first}{\textbf{First} item}.
This is my \MyLabeltext{text:second}{\textit{Second} item}.

Refer back to (\MyTextref{text:first}) and to (\MyTextref{text:second}).

\medskip\hrule\medskip

Something with several paragraphs:

\medskip

Refer forward to \MyTextref{text:third}

This is my \MyLabeltext{text:third}{%
  \textbf{Third} item's first paragraph.\par
  \textbf{Third} item's second paragraph.\par
  \textbf{Third} item's third paragraph.%
}

Refer back to \MyTextref{text:third}

\end{document}

在此处输入图片描述


如果文本短语不包含,\par则执行以下操作:

\documentclass{article}
\usepackage[user]{zref}

\makeatletter
\zref@newprop{MyCurrentText}{}%
\newcommand\MyTextref[1]{\zref[{MyCurrentText}]{#1}}%
\newcommand\MyLabeltext[2]{%
  \zref@setcurrent{MyCurrentText}{#2}%
  \zref@labelbyprops{#1}{MyCurrentText}%
  \MyTextref{#1}%
}%
\makeatother

\begin{document}
Refer forward to (\MyTextref{text:first}) and to (\MyTextref{text:second}).

This is my \MyLabeltext{text:first}{\textbf{First} item}.
This is my \MyLabeltext{text:second}{\textit{Second} item}.

Refer back to (\MyTextref{text:first}) and to (\MyTextref{text:second}).

\end{document}

在此处输入图片描述


如果您希望引用创建指向标签所在位置的超链接,您可以尝试这样的操作 - 为了查看单击超链接后的滚动行为,请以较大的缩放比例查看生成的 .pdf 文件:

\documentclass{article}
\usepackage[colorlinks=true]{hyperref}
\usepackage[user]{zref}

\makeatletter
% Implement some zref-infrastructure in terms of \long :
\@ifdefinable\zref@setcurrentlong{%
  \protected\long\def\zref@setcurrentlong#1#2{%
     \zref@propexists{#1}{\expandafter\def\csname Z@C@#1\endcsname{#2}}%
  }%
}%
\protected\long\def\zref@newlabel{%
  \@newl@bellong{Z@R}%
}%
\newcommand\@newl@bellong[3]{%
  \ifx\@newl@bel\@testdef\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {%
    \def\reserved@a{#3}%
    \expandafter\ifx\csname #1@#2\endcsname\reserved@a\else\@tempswatrue\fi
  }{%
    {%
      \@ifundefined{#1@#2}\relax{%
        \gdef\@multiplelabels{\@latex@warning@no@line{There were multiply-defined labels}}%
        \@latex@warning@no@line {Label `#2' multiply defined}%
      }\global\@namedef{#1@#2}{#3}%
    }%
  }%
}%
\def\ZREF@@@newprop#1{%
  \long\expandafter\gdef\csname Z@E@\ZREF@P\endcsname##1#1##2##3\ZREF@nil{##2}%
}%
%------------------------------------------------------------------------------------------
\zref@newprop{MyCurrentText}{}%
\newcommand\MyTextref{\@ifstar\MyTextrefnolink\MyTextreflink}%
\newcommand\MyTextreflink[1]{\hyperlink{MyText.#1}{\zref[{MyCurrentText}]{#1}}}%
\newcommand\MyTextrefnolink[1]{\zref[{MyCurrentText}]{#1}}%
\newcommand\MyLabeltext[2]{%
  \zref@setcurrentlong{MyCurrentText}{#2}%
  \zref@labelbyprops{#1}{MyCurrentText}%
  \Hy@raisedlink{\hyper@anchorstart{MyText.#1}\hyper@anchorend}%
  \MyTextref*{#1}%
}%
\makeatother

\begin{document}

Links:

Refer forward to (\MyTextref{text:first}) and to (\MyTextref{text:second}).

No links:

Refer forward to (\MyTextref*{text:first}) and to (\MyTextref*{text:second}).

\vfill

This is my \MyLabeltext{text:first}{\textbf{First} item}.

This is my \MyLabeltext{text:second}{\textit{Second} item}.

\vfill

Links:

Refer back to (\MyTextref{text:first}) and to (\MyTextref{text:second}).

No links:

Refer back to (\MyTextref*{text:first}) and to (\MyTextref*{text:second}).

\vfill

Something with several paragraphs:

\medskip

Links:

Refer forward to \MyTextref{text:third}

No links:

Refer forward to \MyTextref*{text:third}

\vfill

This is my \MyLabeltext{text:third}{%
  \textbf{Third} item's first paragraph.\par
  \textbf{Third} item's second paragraph.\par
  \textbf{Third} item's third paragraph.%
}

\vfill

Links:

Refer back to \MyTextref{text:third}

No links:

Refer back to \MyTextref*{text:third}

\end{document}

在此处输入图片描述

.pdf 文件中目标名称的模式为 MyText.<referencing-label>。

相关内容