首页 > 解决方案 > 如何在 LaTex 中为引用添加前缀?

问题描述

所以我有这样的数字:

\begin{figure}[h]{}
    \includegraphics[width=\textwidth]{./assets/demo/identify-usecases/DomainSplitt-1.jpg}
    \caption{Identifying Domains: Match History with Filter}
    \centering
    \label{fig:domainsplitt-one}
\end{figure}

我这样引用它们:\ref{fig:domainsplitt-one}.

在生成的 PFD 中,图像具有这样的标题Figure 4.13: Identifying Domains: Match History with Filter。但文中的参考只有数字4.13

我有类似的代码:

\begin{code}
    \captionof{listing}{Custom Element listening to global custom events}
    \label{code:listening-custom-event}
    \begin{minted}{JavaScript}

在这里,代码有前缀Source Code,但参考中只有数字。我使用的规则是

\usepackage[newfloat]{minted}
\usepackage{caption}
\newenvironment{code}{\captionsetup{type=listing}}{}
\SetupFloatingEnvironment{listing}{name=Source Code}

所以我需要的是,参考在文本中也有这个前缀。由于我对乳胶真的很陌生,可能会问:我该怎么做?

编辑:这里是一个副本的链接: https ://github.com/adrian-goe/latex-replica

标签: latex

解决方案


cleveref软件包将为您完成所有工作:

% !TeX program = txs:///arara
% arara: pdflatex: {synctex: on, interaction: nonstopmode, shell: yes}


% Preamble
%\documentclass[11pt]{article}

% Packages
\documentclass[12pt,DIV12,BCOR0mm,twoside,openright,headings=normal,    numbers=noenddot,headsepline,headinclude]{scrreprt}
\usepackage[automark,headsepline]{scrlayer-scrpage}
\usepackage{scrhack}                % to avoid KOMA-Script warning
\usepackage[utf8]{inputenc}         % UTF8 encoding
\usepackage[T1]{fontenc}
\usepackage{txfonts}                % Postscript fonts
\usepackage[ngerman,english]{babel}
\usepackage[style=numeric-comp,giveninits=true,maxnames=10,sorting=none]{biblatex}
\usepackage{graphicx}
\usepackage[usenames]{xcolor}
\usepackage[pdftex]{hyperref}
\usepackage{wallpaper}

\usepackage[newfloat]{minted}
\usepackage{caption}
\newenvironment{code}{\captionsetup{type=listing}}{}
\SetupFloatingEnvironment{listing}{name=Code}
\usemintedstyle{manni}

\usepackage[noabbrev]{cleveref}

\crefname{listing}{code}{code}
\Crefname{listing}{Code}{Code}

% Document
\begin{document}
    this is my sample text and i want to reference \cref{code:update-a-attribute} and the \cref{fig:image}
    \begin{figure}[h]{}
        \includegraphics[width=\textwidth]{./img.png}
        \caption{some image}
        \centering
        \label{fig:image}
    \end{figure}

    \begin{code}
        \captionof{listing}{Update a Attribute}
        \label{code:update-a-attribute}
        \begin{minted}{JavaScript}
const fragment = document.querySelector(".fragment");
fragment.setAttribute("data", 2)
        \end{minted}
    \end{code}

\end{document}

在此处输入图像描述

(请注意,您只能拥有一个文档类)


推荐阅读