![](/img/trans.png)
[英]Label not working inside a \newcommand with a custom Float in LaTeX
[英]How to reference a label within a newcommand in LATEX?
我創建了一個新命令\\ figc來快速為文檔創建圖形。 但是,我在引用它時遇到了麻煩。 該文檔已成功編譯,但如圖(??)所示。
\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}
,而應使用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}
,因此可以提供未定義的引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.