在 LaTeX 中,有没有办法将图形的文件名包含到标题中?

发布于 2024-09-19 21:21:18 字数 322 浏览 4 评论 0原文

我有一个 LaTeX 文档,其中包含来自 \includegraphics{bla.pdf} 的外部文件的大量绘图。绘图是在 Inkscape 中完成的。这一切都很好。然而,有时很难跟踪所有图纸及其源文件。因此,我正在寻找一种将源文件名(在示例 bla.pdf 中)包含到标题中的方法。作为一名 LaTeX 专家,我还没有找到一种方法来自动访问标题中的文件名字符串。使用 Google-foo 也没有产生结果。是否有现有的方法来引用文件名并将其包含在标题中,例如 \caption{A fancy Drawing of bla (\filename}?

I have a LaTeX document with a large number of drawings included from external files with \includegraphics{bla.pdf}. The drawings are done in Inkscape. This all works fine. However sometimes it is hard to keep track of all the drawings and their source files. Hence I'm looking for a way to include the source file name, in the example bla.pdf, into the caption. Not being a LaTeX expert I haven't found a way to automatically access the filename string from the caption. Using Google-foo didn't yield a result either. Is there an existing way to refer to the filename and include it in the caption, like \caption{A fancy drawing of bla (\filename}?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┊风居住的梦幻卍 2024-09-26 21:21:18

您可以通过修补 \includegraphics 内的部分嵌套调用来捕获图像文件名:

在此处输入图像描述

\documentclass{article}

\usepackage{graphicx,etoolbox}

\makeatletter
\patchcmd{\Gin@setfile}% <cmd>
  {\ProvidesFile}% <search>
  {\xdef\imgfilename{#3}\ProvidesFile}% <replace>
  {}{}% <success><failure>
\makeatother

\begin{document}

\begin{figure}
  \centering
  \includegraphics[width=.5\linewidth]{example-image}
  \caption{Image: \imgfilename}
\end{figure}

\end{document}

\imgfilename 将包含 \includegraphics 仅在使用后使用。

You can capture the image filename by patching a part of the nested calls inside \includegraphics:

enter image description here

\documentclass{article}

\usepackage{graphicx,etoolbox}

\makeatletter
\patchcmd{\Gin@setfile}% <cmd>
  {\ProvidesFile}% <search>
  {\xdef\imgfilename{#3}\ProvidesFile}% <replace>
  {}{}% <success><failure>
\makeatother

\begin{document}

\begin{figure}
  \centering
  \includegraphics[width=.5\linewidth]{example-image}
  \caption{Image: \imgfilename}
\end{figure}

\end{document}

\imgfilename will contain the filename used in \includegraphics only after it's used.

方觉久 2024-09-26 21:21:18

重新定义\includegraphics。对于没有参数的情况(宽度=3厘米,比例=2或类似的东西),我们有

\let \saveinclude \includegraphics
\def \includegraphics#1{\def\filename{#1}\saveinclude{#1}}

使用

\includegraphics{bla.pdf}
\caption{A fancy drawing of bla (\filename)}

Redefine \includegraphics. For the case of no arguments (width=3cm, scale=2 or something like this) we have

\let \saveinclude \includegraphics
\def \includegraphics#1{\def\filename{#1}\saveinclude{#1}}

The use

\includegraphics{bla.pdf}
\caption{A fancy drawing of bla (\filename)}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文