
尊敬的專家:如何寫出表格標題以供日後使用?思考
\documentclass{article}
\newcommand{\mycaption}[2]{\caption{#2}\label{#1}\savecaptioncontent{#1}{#2}}
\newcommand{\insertmytable}[1]{
\begin{center}
Insert Table~\ref{#1} here: '\contentcaption{#1}'
\end{center}
}
\begin{document}
blah blah. Table~\ref{tbl:first} tells me little.
\insertmytable{tbl:first}
above, it should have printed:
\begin{center}
Insert Table~\ref{tbl:first} here: 'This is my first table'
\end{center}
\begin{table}
\mycaption{tbl:first}{This is my first table}
\end{table}
\end{document}
幾乎可以肯定我想寫入該.aux
文件,但我的業餘嘗試陷入了擴展地獄。事實上,對於這種特殊情況可能有一個更簡單的解決方案:我認為該文件已經使用when.aux
保存了標題(什麼時候不是?),但我需要從 aux- 中取出第三個帶括號的表達式寫的。我不想進一步猜測,而是希望這是一個足夠常見的問題,很容易並且成為一個很好的 sx 答案...\newlabel
hyperref
\newlabel
幫助表示讚賞。 /iaw
答案1
這是一個「骯髒」的技巧:使用新標籤的前綴\newlabel
明確寫入文件,並使用from來獲取標籤內容。.aux
stored::
\nameref*
hyperref
{}{}
和參數{}
在這裡可以為空,因為它們對於這種方法並不重要。
然而,擴張將是一個問題。如果標題內容本身包含非常簡單的命令,則\unexpanded{}
應該使用。更好的解決方案取決於精確的要求。
\documentclass{article}
\usepackage{caption}
\usepackage{hyperref}
\makeatletter
\newcommand{\savecaptioncontent}[2]{%
\immediate\write\@auxout{%
\string\newlabel{stored::#1}{{}{}{\unexpanded{#2}}{}}%
}
}
\newcommand{\contentcaption}[1]{%
\nameref*{stored::#1}%
}
\makeatother
\newcommand{\mycaption}[2]{\caption{#2}\label{#1}\savecaptioncontent{#1}{#2}}
\newcommand{\insertmytable}[1]{
\begin{center}
Insert Table~\ref{#1} here: '\contentcaption{#1}'
\end{center}
}
\begin{document}
blah blah. Table~\ref{tbl:first} tells me little.
But now:
\insertmytable{tbl:first}
above, it should have printed:
\begin{center}
Insert Table~\ref{tbl:first} here: 'This is my first table'
\end{center}
\begin{table}
\mycaption{tbl:first}{This is my first table}
\end{table}
\end{document}