如何使用子文件从主图像文件夹的子目录中的 /includegraphics

如何使用子文件从主图像文件夹的子目录中的 /includegraphics

由于我暂时无法评论帖子,所以我必须问这个问题。以下是带有示例的链接:(markellos 的最后一个答案是我使用和参考的)

带有子文件 latex 的不同目录中的图形路径

他的案例:

主文本:

\providecommand{\main}{.} 
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}

%here is the path
\graphicspath{{\main/images/}{images/}}

\usepackage{subfiles}

\usepackage{blindtext}

\begin{document}

\subfile{\main/chap1/chap1}

\end{document}

第一章.tex

\providecommand{\main}{..}
\documentclass[\main/main.tex]{subfiles}

\begin{document}

\begin{figure}[bh]
\centering
\includegraphics[width=4cm]{f1}

\label{fig:img1}
\caption{ShareLaTeX learn logo}
\end{figure}

Hello, here is some text...
\end{document}

因此,假设我的主要图像目录是“图像”,并且您可以使用以下方式调用它:(\includegraphics[width=4cm]{./f1}至少我这样做)

但是我在“图像”中还有一个名为“ABC”的子文件夹......如何使用 \includegraphics 从中调用?

我不太擅长使用 latex,我主要从答案和其他模板中复制内容,然后根据自己的喜好进行混合。我正在写论文,我将有几个其他包含图像的子文件夹(我正在做组织学)。因此,将它们分类到与主“图像”文件夹不同的文件夹中会非常方便。

我尝试过\includegraphics{../f1),但是有东西告诉我必须修改 main.tex 中的此行以包含我的子文件夹:

\graphicspath{{\main/images/}{images/}}

但如何呢?

答案1

请原谅我用家庭做类比,但它可能有助于你了解你的亲戚是如何构成的,以及他们是如何被提及的

因此,您是一个文件夹,我们称您为 Thomas,并且您有一个父级,我们称他们为 blindhardt,您在系统中被放置为 /blindhardt/thomas,但为了自我引用,您可以使用别名“。”

您有孩子 /blindhardt/thomas/janet 和 /blindhardt/thomas/john,
他们可能有昵称,例如 preface 和 chap1,
以表明您的孩子与您的关系,您可以说 ./preface 或 ./chap1
表示您的父亲,您可以说 ..,对于兄弟姐妹,他们可能是 ../年长或 ../年幼,
您的家谱现在可以

/blindhardt/older
           /thomas/
           /thomas/preface
           /thomas/chap1
           /younger

好的,这是你的相对路径的基本理论,现在回答你的问题,
你托马斯的一部分,家庭圣经的保管人,被称为main.tex

markellos 表现得很出色如果你的那部分(main.tex)被识别为

\providecommand{\main}{.}

那么任何 ../child/story.tex 都可以将你视为其提供者

\providecommand{\main}{..}   i.e their own parent

还有他们的补充声明

\documentclass[\main/main.tex]{subfiles}

确认他们和你属于同一子组(类别)

作为家人,您同意将所有图片资产保存在
/blindhart/somewhere/vault 中,但作为保管人,您希望如何称呼它

/blindhart/thomas/images

% 这是您的保险库路径 ./images 和下一代 {\main\images/}

\graphicspath{{images/}{\main/images/}}

好的,到目前为止还没有什么新东西,假设你在那个保险库中保存了一个文件夹(ABC),
你和你的孩子现在都需要有一些交叉索引。

 \graphicspath{{images/}{\main/images/}{images/ABC/}{\main/images/ABC/}}

因此,现在无论 pic1.png 或 fig1.pdf 位于 main/images 还是 /images/ABC,
您的所有直系文件都可以通过以下方式找到它:

\includegraphics[width=4cm]{pic1}

Main.tex(仅修改一行)

\providecommand{\main}{.} 
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}

%here is the NEW path
\graphicspath{{images/}{\main/images/}{images/ABC/}{\main/images/ABC/}}

\usepackage{subfiles}
\usepackage{blindtext}
\begin{document}
\subfile{\main/chap1/chap1}
\end{document}

chap1.tex(没有变化,只是被更广泛地接受)

\providecommand{\main}{..}
\documentclass[\main/main.tex]{subfiles}
\begin{document}
\begin{figure}[bh]
\centering
% f1 can be any accepted image (png pdf etc.) beware if there are two in different locations then the order WILL become important.
% it can be in ./f1.png it may be in ../images/f1.pdf or even ../images/ABC/f1.eps
\includegraphics[width=4cm]{f1}
\label{fig:img1}
\caption{ShareLaTeX learn logo}
\end{figure}
Hello, here is some text...
\end{document}  

显然 /older 及其 /children 可以遵循相同的模式,但如果你们都想访问 /blindhardt/images 中的家族徽章,那么你们就开始添加更多嵌套的相对层,并且可能更容易为这些主图像包含绝对地址,因此你们和兄弟姐妹可以添加

\graphicspath{{c:\familydocs\blindhardt\images/}{images/}{\main/images/}{images/ABC/}{\main/images/ABC/}}  

有办法在上面包含空格,但为了完全兼容,我建议不要包含任何空格

以上所有内容均基于您问题的措辞,但是每个 tex 文件都可以自行访问公共库。

您的${TEXMFHOME}/tex/generic/images

您还可以通过变量指定一系列项目集合TEXINPUTS=".:.//:c:\road\to\nowhere: ",如果您知道自己在做什么,则可以在每次项目运行之前重新定义不同的方法。

注意特殊标记 .// 表示所有子目录

自动定位包含的图像重命名章节文件夹并有效引用图像

答案2

欢迎来到 tex.SE

对于一个更大的项目,我不久前编写了一些可能对您有帮助的东西。

基本思想是在开始和结束处添加一个钩子\input,并\include自动调整图形路径。既然\subfile使用了它\input,它应该也能正常工作。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{blindtext}

% use parent management with current source file 
\usepackage{subfiles} 
\usepackage{filehook} 
\usepackage[parent]{currfile} 

\makeatletter
% Define a new stack by letting it to \@empty 
\newcommand\newStack[1]{\global\let#1\@empty} 
% add element to stack (top): \addStack{<stack>}{<element>} 
\newcommand{\addStack}[2]{\def\ele@ment{{#2} }\xdef#1{\ele@ment #1}} 
% add element to stack (bottom) \revaddStack{<stack>}{<element>} 
\newcommand{\revaddStack}[2]{\def\ele@ment{{#2} }\xdef#1{#1 \ele@ment}} 
% remove from stack
\long\def\pop@#1 #2\@nil#3{\def\popedStackElement{#1}%
  \def#3{#2}}% remaining list 
% stack name #1 
\newcommand\popStack[1]{\let\popedStackElement\@empty% 
  \ifx #1\@empty\message{LaTeX warning: you are trying to remove an element from the empty stack \noexpand#1 on input line \the\inputlineno}\else\expandafter\pop@#1\@nil#1\fi} 
% whipe stack 
\newcommand\cleanStack[1]{\global\let#1\@empty} 

%%% enhanced graphicspath management 
% idea: everytime \includegraphics is called from a file of a (larger) project the graphics file is searched 
%       in a specific folder relative to that one the actual tex source code resides in. 
%       In addition generally valid paths can be defined. 
% idea taken from https://tex.stackexchange.com/questions/45946/stack-datastructure-using-latex ;
% uservariant and autovariant of graphicspath stack 
\newStack{\GrapPa@user} 
\newStack{\GrapPa@auto} 
% activate stack as graphicspath 
% def hook for entering file: add path and update graphicspath 
\newcommand{\grappa@start@hook}{%
  \ifcurrfiledir\@empty{}{\addStack{\GrapPa@auto}{\currfiledir\graphicsfolder}}\update@grappa} 
% def hook for leaving file: remove path and update graphicspath 
\newcommand{\grappa@end@hook}{%
  \ifcurrfiledir\@empty{}{\popStack{\GrapPa@auto}}\update@grappa}
% activate stack as graphicspath 
\newcommand{\update@grappa}{\xdef\GrapPa@temp{{\GrapPa@user \GrapPa@auto}}\expandafter\graphicspath\GrapPa@temp} 
%
% user space graphicspath stack: 
\newcommand{\newaddgraphicspath}[1]{\addStack{\GrapPa@user}{#1}\update@grappa} 
\newcommand{\removelastgraphicspath}{\popStack{\GrapPa@user}\update@grappa} 
\newcommand{\showgraphicspath}{\message{user: \show\GrapPa@user\space - auto: \show\GrapPa@auto}} 
% define default graphics folder name
\newcommand{\graphicsfolder}{images/}
% now register for linking to filehook 
\AtBeginDocument{%
  \AtBeginOfInputs{\grappa@start@hook}%
  \AtBeginOfIncludes{\grappa@start@hook}%
  \AtEndOfInputs{\grappa@end@hook}%
  \AtEndOfIncludes{\grappa@end@hook}} 
%%% enhanced graphicspath management
\makeatother

\newaddgraphicspath{./\graphicsfolder} 

\begin{document}
\subfile{./chap1/chap1}
\end{document}

通过这个\inputs 和\includes 可以嵌套,并且图形文件将加载到 relativ 指定的\graphicsfolder相对于 tex 文件所在目录的文件夹中。

如果您有其他全局路径,您可以通过调用来添加它们\newaddgraphicspath。但是使用\removelastgraphicspath比较棘手,除非您知道自己在做什么,否则应该避免使用它。

调用\showgraphicspath将为您提供在日志文件和控制台中搜索的实际路径列表。

和....相比这个伟大而简单的答案此解决方案具有以下特点:如果在同一级别找不到图像,则还会搜索所有级别 - 因此将图像放在第一级的图像文件夹中可以访问此分支中的所有后续级别。例如,您可以使用它向不同部分和级别添加不同的标记,而无需复制深层嵌套的图像文件或更改宏。

相关内容