サブファイルを使用してメイン画像フォルダのサブディレクトリから /includegraphics を実行する方法

サブファイルを使用してメイン画像フォルダのサブディレクトリから /includegraphics を実行する方法

まだ投稿にコメントできないので、この質問をしなければなりません。例のリンクはこちらです: (markellos による最後の回答は私が使用し、参照しているものです)

サブファイル latex を持つ異なるディレクトリ内の図のパス

彼の場合:

メイン.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}

チャップ1.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}

メインの画像ディレクトリが「images」で、次のように呼び出すとします\includegraphics[width=4cm]{./f1}(少なくとも私はそうしています) 。

しかし、「images」内に「ABC」というサブフォルダーもあります。\includegraphics を使用してそこから呼び出すにはどうすればよいでしょうか?

私は LaTeX があまり得意ではないので、主に回答や他のテンプレートからコピーして自分の好みに合わせて組み合わせています。私は論文を書いていますが、画像を含むサブフォルダがいくつかあります (組織学を研究しています)。そのため、メインの「画像」フォルダとは別のフォルダに整理しておくと非常に便利です。

試してみました\includegraphics{../f1)が、サブフォルダーを含めるには main.tex のこの行を変更する必要があるようです。

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

しかし、どうやって?

Br

答え1

家族の例えはご容赦ください。しかし、これはあなたの親戚がどのように構成され、どのように参照されているかを理解するのに役立つかもしれません。

つまり、あなたはフォルダであり、親はblindhardtです。システムでは/blindhardt/thomasとして配置されますが、自己参照にはエイリアスを使用できます。「。」

あなたには/blindhardt/thomas/janetと/blindhardt/thomas/johnという子供がいます。
彼らはprefaceやchap1のようなニックネームを持っているかもしれません
。あなたとあなたの子供の関係を示すために、./prefaceまたは./chap1と言うことができます
。あなたの父親の場合は..と言うことができます。兄弟の場合は../年上または../年下である可能性があります。
あなたの家系図は次のようにすることができます。

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

さて、これが相対パスの基本理論です。それではあなたの答えに移りましょう。
あなたの一部であるトーマス(家族の聖書の管理者)はmain.texとして知られています。

マークロスはきれいに表示されますもしあなたのその部分(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/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 (1行だけ変更)

\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}

これにより、\input\includeをネストすることができ、グラフィック ファイルは、\graphicsfolderTex ファイルが存在するディレクトリに対して relativ で指定されたフォルダーにロードされます。

追加のグローバル パスがある場合は、 を呼び出して追加できます\newaddgraphicspath。ただし、 の使用は\removelastgraphicspath注意が必要なので、何をしているのか理解していない限りは使用を避ける必要があります。

呼び出すと、\showgraphicspathログファイルとコンソールで検索された実際のパスのリストが表示されます。

と比べてこの素晴らしいシンプルな答えこのソリューションには、同じレベルで画像が見つからない場合に、すべての上位レベルも検索されるという機能があります。そのため、最初のレベル内の画像フォルダーに画像を配置すると、このブランチのすべての後続レベルにアクセスできるようになります。これを使用すると、たとえば、深くネストされた画像ファイルをコピーしたり、マクロを変更したりすることなく、さまざまな部分やレベルにさまざまなマーカーを追加できます。

関連情報