Cross reference of Table, Figure etc

Preparing APS manuscripts, it is required that when "Table, Figure" appear at the beginning of a sentence, they should appear in their full form, otherwise, in abbreviated form as "Tab., Fig.".

Using \ref{} one needs to decide and type the referred names by hand; on the other hand, using \autoref{}, which though automatically brings the referred names, it can not, as far as I know, tell whether the names appear at the beginning of a sentence or not.

So, is there an automated way to meet the above-mentioned requirement, instead of everytime inputting by hand?

• cleveref has \cref and \Cref. The two forms usually give different capitalisation, so \Cref is usually used at the beginning of a sentence and \cref in the middle. It should be possible to modify the involved strings so that \cref shows "Tab." and \Cref "Table". – moewe Dec 22 '18 at 12:42

As long as in \nonfrenchspacing you can fork depending on the \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}


• As far as I can see this otherwise very nice solution does not give the long form at the beginning of a new paragraph. In cases like this spacefactor solutions would be too fragile for me and I would recommend using different commands (like \cref vs. \Cref or \cite vs. \Cite). – moewe Dec 25 '18 at 12:29

Extending the example provided by Ronald Klopp I suggest implementing an \autoref-variant which does check for hmode also.

As a side-effect this provides the possibility of "fooling" the test into the "full" form by prepending \therewashmodefalse to \hmodeckeckautoref. I think you can fool the test into the short form by prepending \null to \hmodeckeckautoref.

As another side effect this might resolve some of the situations where you get the message
! Improper \spacefactor.

I advice to use this with care and to carefully check whether things turn out as desired.

\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}