如何在LATEX中的newcommand中引用标签? [英] How to reference a label within a newcommand in LATEX?

查看:40
本文介绍了如何在LATEX中的newcommand中引用标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新命令\ figc以快速为文档创建图形.但是,我在引用它时遇到了麻烦.该文档已成功编译,但显示为图(??).

I have created a newcommand \figc to create figures quickly for my document. However, I had trouble referencing it. The document is being compiled successfully but is being shown as Figure (??).

\newcommand{\figc}[3]{    
\begin{figure}[H]
    \centering
    \includegraphics[width={#3}]{figures/{#1}.jpg}
    \caption{{#2}}
    \label{fig:{#1}}
\end{figure}
    fig:{#1}}    ----> However, this part is displayed correctly in the PDF as fig:samplefig


Here is a sample text to reference (Figure \ref{fig:samplefig}).
\figc{samplefig}{Sample Figure}{3in}

推荐答案

请勿在 \ label 中使用 fig:{#1} ,而应使用<代码>图:#1 :

Don't use fig:{#1} in your \label, but instead use fig:#1:

\documentclass{article}

\usepackage{float,graphicx}

\newcommand{\figc}[3]{%
  \begin{figure}[H]
    \centering
    \includegraphics[width=#3]{#1.jpg}
    \caption{#2}
    \label{fig:#1}
  \end{figure}
  fig:#1% Set the reference as well
}

\begin{document}

Here is a sample text to reference (Figure \ref{fig:example-image}).

\figc{example-image}{Sample Figure}{3in}

\end{document}

文档中设置的内容与其内部表示形式之间存在差异.因此, \ label {fig:{abc}} \ label {fig:abc} 不同,因此可以提供未定义的引用.

There's a difference between what is set in a document, and its internal representation. That's why \label{fig:{abc}} is different from \label{fig:abc}, and therefore can provide an undefined reference.

这篇关于如何在LATEX中的newcommand中引用标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆