Autoref:在标签前显示计数器

Autoref:在标签前显示计数器

这可能看起来很奇怪,但它是特定于文化的。是否可以以某种方式更改 autoref 以呈现“1 Figure”而不是“Figure 1”?

答案1

作为不同类型浮点数(图形、表格、算法等)的通用解决方案,您可以\autoref使用数字 from\ref后跟浮点数名称(不带数字)来重新定义 from \nameCref(由包提供cleveref)。如果您还想更改标题,那么您可以使用包声明自定义格式caption

梅威瑟:

\documentclass{article}
\usepackage{caption}
\DeclareCaptionLabelFormat{switched}{#2 #1}
\captionsetup{labelformat=switched}
\usepackage{hyperref}
\usepackage{cleveref}
\renewcommand{\autoref}[1]{\hyperref[#1]{\ref*{#1} \nameCref{#1}}}

\begin{document}

\begin{figure}
\centering
\fbox{this is a figure}
\caption{a caption}
\label{fig:example}
\end{figure}

See \autoref{fig:example} for an example.

\end{document}

结果:

在此处输入图片描述

答案2

我在这里找到了这个主题: 如果参数以 # 开头,则将其吞噬

我对其进行了修改,以便为您提供所需的结果。

代码如下:

\makeatletter
% this is found in latex.ltx:
% \def\@car#1#2\@nil{#1}
\def\iffirsttoken#1#2{%
  % define \@first@token to be the once expanded \@car of the first argument
  % i.e. the first token or balanced group:
  \expandafter\def\expandafter\@first@token\expandafter{\@car#1\@nil}%
  % test if the expansion of \@first@token is the same as #2:
  \expandafter\ifx\@first@token#2\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}

\usepackage{hyperref}

\let\oldhref\autoref
\renewcommand{\autoref}[1]{%
  %\oldhref{#1}%
  \iffirsttoken{#1}{t}{~\ref{#1} Table}%
}

并且您的表格(仅它们)必须以字母 t 作为其标签的首字母......

测试一下...您可以扩展图形等的代码

編輯 MWE:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}



\makeatletter
% this is found in latex.ltx:
% \def\@car#1#2\@nil{#1}
\def\iffirsttoken#1#2{%
  % define \@first@token to be the once expanded \@car of the first argument
  % i.e. the first token or balanced group:
  \expandafter\def\expandafter\@first@token\expandafter{\@car#1\@nil}%
  % test if the expansion of \@first@token is the same as #2:
  \expandafter\ifx\@first@token#2\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}

\usepackage{hyperref}

\let\oldhref\autoref
\renewcommand{\autoref}[1]{%
  %\oldhref{#1}%
  \iffirsttoken{#1}{t}{~\ref{#1} Table}\relax%
  \iffirsttoken{#1}{f}{~\ref{#1} Figure}%
}

\title{}
\author{}

\begin{document}



\begin{table}
 \centering
 \caption{}\label{tst}
 \begin{tabular}{ c c}
\hline
 Here & is\\
  a & table\\\hline
 \end{tabular}

\end{table}

We can refer to the table as Table \ref{tst}
with the \verb|\ref| command or to refer with 
\verb|\autoref| and have \autoref{tst}

\begin{figure}
 \centering
 \caption{}\label{fst}
 \begin{tikzpicture}
  \draw[fill=blue] (0,0) rectangle (2,1);
 \end{tikzpicture}
\end{figure}

We can refer to the figure as Figure \ref{fst}
with the \verb|\ref| command or to refer with 
\verb|\autoref| and have \autoref{fst}

\end{document}

结果

在此处输入图片描述

相关内容