如何使用子檔案從主圖像資料夾的子目錄 /includegraphics

如何使用子檔案從主圖像資料夾的子目錄 /includegraphics

由於我還不能評論帖子,所以我不得不問這個問題。這是範例的連結:(markellos 的最後一個答案是我使用和參考的內容)

具有子文件乳膠的不同目錄中的圖形路徑

他的案例:

主要.tex:

\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}至少我這樣做)

但我在“images”中還有一個名為“ABC”的子資料夾。

我不太擅長乳膠,我主要從答案和其他模板中複製內容,以根據自己的喜好進行混合。我正在寫我的論文,我將有幾個其他包含圖像的子資料夾(我正在做組織學)。因此,將它們分類到主“圖像”資料夾的不同資料夾中會非常方便。

我嘗試過\includegraphics{../f1),但有些東西告訴我,我必須修改 main.tex 中的這一行以包含我的子資料夾:

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

但如何呢?

答案1

請原諒家庭類比,但這可能會幫助您了解親戚的結構以及他們的提及方式

所以你是一個資料夾,我們稱你為 Thomas,你有一個父級,我們稱他們為blindhardt,你被放置在系統中為 /blindhardt/thomas,但為了自我參考,你可以使用別名“。”

你有孩子 /blindhardt/thomas/janet 和 /blindhardt/thomas/john
他們可能有暱稱,如序言和 chap1
來表明你的孩子與你自己的關係,你可以說 ./preface 或 ./chap1
代表你的父親,你可以說..對於兄弟姐妹,他們可能是../年長或../年輕,
您的家譜現在可以是

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

好吧,這就是你現在到答案的相對路徑的基本理論

馬凱洛斯表現得很整齊如果你的那部分(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

% 這裡是你的 Vault 路徑 ./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 可以嵌套,並且圖形檔案將載入到由\graphicsfolder相對於 tex 檔案所在目錄指定的資料夾中。

如果您有其他全域路徑,您可以透過呼叫來新增它們\newaddgraphicspath。然而使用\removelastgraphicspath是很棘手的,除非你知道自己在做什麼,否則你應該避免使用它。

呼叫\showgraphicspath將為您提供在日誌檔案和控制台中搜尋的實際路徑清單。

和....相比這個偉大而簡單的答案該解決方案的特點是,如果在同一層級找不到圖像,也會搜尋所有層級 - 因此將圖像放入第一層級的圖像資料夾中可以存取該分支中的所有後續層級。例如,您可以使用它向不同的部分和層級添加不同的標記,而無需複製深度嵌套的圖像檔案或更改巨集。

相關內容