编写APS稿件时,要求当“Table, Figure”出现在句首时,应以完整形式出现,否则,以缩写形式出现为“Tab., Fig.”。
使用时\ref{}
,需要手动决定并输入所指的名称;另一方面,使用\autoref{}
,虽然会自动带来所指的名称,但据我所知,它无法判断这些名称是否出现在句子的开头。
那么,有没有一种自动化的方法来满足上述要求,而不用每次都手动输入?
答案1
只要\nonfrenchspacing
你可以根据以下内容进行分叉\spacefactor
:
\documentclass{article}
\usepackage{hyperref}%
\makeatletter
\newcommand\sfcodefork{%
\ifnum\the\spacefactor=1000 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}%
\renewcommand\figureautorefname{\sfcodefork{Fig.\texorpdfstring{\null}{}}{Figure}}
\renewcommand\tableautorefname{\sfcodefork{Tab.\texorpdfstring{\null}{}}{Table}}
\makeatother
\begin{document}
\nonfrenchspacing
\section{A section}
\begin{figure}%
\rule{1cm}{1cm}%
\caption{caption of figure.}\label{label of figure}%
\end{figure}%
\begin{table}%
\begin{tabular}{ll}
left&right\\
\end{tabular}
\caption{caption of table.}\label{label of table}%
\end{table}%
References at beginning of sentence:
\autoref{label of figure} is referenced.
\autoref{label of table} is referennced.
We can refer \autoref{label of figure} in the middle of a sentence.
We can refer \autoref{label of table} in the middle of a sentence.
\end{document}
答案2
扩展示例由 Ronald Klopp 提供我建议实现一个\autoref
-variant,它也检查 hmode。
作为副作用,这提供了通过在前面添加 来“欺骗”测试为“完整”形式的可能性\therewashmodefalse
。\hmodeckeckautoref
我思考\null
您可以通过在前面添加来欺骗测试,使其变为简短形式\hmodeckeckautoref
。
另一个副作用是,这可能会解决您收到消息的一些情况
! Improper \spacefactor.
我建议小心使用它并仔细检查事情是否按预期进行。
\documentclass{article}
\usepackage{hyperref}%
\makeatletter
\newif\iftherewashmode\therewashmodetrue
\newcommand\hmodeckeckautoref{\ifhmode\else\global\therewashmodefalse\fi\autoref}
\newcommand\sfcodefork{%
\iftherewashmode\expandafter\@firstoftwo\else\global\therewashmodetrue\expandafter\@secondoftwo\fi{%
\ifnum\spacefactor=1000 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}{\@secondoftwo}%
}%
\renewcommand\figureautorefname{\sfcodefork{Fig.\texorpdfstring{\null}{}}{Figure}}
\renewcommand\tableautorefname{\sfcodefork{Tab.\texorpdfstring{\null}{}}{Table}}
\makeatother
\begin{document}
\nonfrenchspacing
\section{A section}
\begin{figure}%
\rule{1cm}{1cm}%
\caption{caption of figure.}\label{label of figure}%
\end{figure}%
\begin{table}%
\begin{tabular}{ll}
left&right\\
\end{tabular}
\caption{caption of table.}\label{label of table}%
\end{table}%
References at beginning of sentence:
\hmodeckeckautoref{label of figure} is referenced.
\hmodeckeckautoref{label of table} is referennced.
References at beginning of paragraph:
\hmodeckeckautoref{label of figure} is referenced.
\hmodeckeckautoref{label of table} is referennced.
We can refer \hmodeckeckautoref{label of figure} in the middle of a sentence.
We can refer \hmodeckeckautoref{label of table} in the middle of a sentence.
\end{document}