如何从复杂的文件结构中包含图形

如何从复杂的文件结构中包含图形

我正在编写一个 Latex 文件,其中需要包含目录图中的图形。

图表有几个子目录,比如说,。这些单独包含应包含在文件中的几个图形。

一种方法是手动将每个图形都包含在文件中。但是,这太麻烦了。有没有更好的方法呢?

答案1

在你的序言中写到:

\graphicspath{{graphs}{graphs/one}{graphs/two}{graphs/three}}

如果您没有太多子目录,则这样做是有意义的。每个路径都括在括号中。

答案2

这是一个解决方案。稍后将添加评论。

\documentclass[a4paper,10pt]{article}
\usepackage{graphicx,ifpdf}
\usepackage{ltxkeys}[2011/12/10]
\makeatletter
\ltxkeys@declarekeys*[KV]{importfiles}[fim@]{
  cmd/setorlaunch/set;
  cmd/processor/\includegraphics/
    \ifltxkeys@dec\else
      \ifescapedTF{#1}{}{
        \edef\fim@processor{\noexpandcsn\fim@processor}
      }
      \def\reserved@a##1{
        \def\reserved@a####1##1####2####3\@nil{
          \def\fim@procnr{####2}
          \ifx\fim@procnr\@nnil
            \@latex@error{Unknown processor \unexpanded{##1}}\@ehc
          \fi
        }%
        \reserved@a\includegraphics{0}\input{1}\include{2}##1{\@nil}\@nil
      }%
      \s@expandarg\reserved@a\fim@processor
    \fi;
  cmd/stuffbefore//;
  cmd/stuffafter//;
  cmd/fileext/tex/
    \ifpdf
      \ifinsetTF{#1}{,pdf,png,jpeg,jpg,mps,}
        {
          \edef\fim@graphicsext{#1}
        }{
          \def\fim@graphicsext{pdf}
        }
    \else
      \def\fim@graphicsext{eps}
    \fi;
  cmd/inputpaths/./
    \ifltxkeys@dec\else
      \def\fim@inputpaths{}
      \edef\fim@tempa{#1}
      \fim@stripbraces\fim@tempa
      % \openin will not take spaces in path or filename:
      \edef\fim@tempa{\s@expandarg\cptzapspaces\fim@tempa}
      \docommalist*\fim@tempa{
        \xifinsetTF{/\relax}{##1\relax}{
          \edef\fim@inputpaths{\fim@inputpaths\ifbracedTF{##1}{##1}{{##1}}}
        }{
          \@latex@error{Inputpath '##1' not ended with slash (/)}\@ehc
        }
      }
    \fi;
  cmd/inputpath/./\setaliaskey{inputpaths};
  cmd/scale/1;
  cmd/\needvalue{width}//;
  cmd/\needvalue{height}//;
  cmd/\needvalue{viewport}/0 0 100 100;
  bool/clip/true;
  choice/align.{
    center/\def\fim@align{center},
    left/\def\fim@align{flushleft},
    right/\def\fim@align{flushright},
    justified/\def\fim@align{fim@phantomenv}
  }/center;
}
% Why didn't I want to say \let\fim@phantomenv\relax?
\new@def*\fim@phantomenv{\relax}
\robust@def*\fim@stripbraces#1{%
  \cptstripallbrincs#1\despacecontent#1\cptstripallbrincs#1%
}
\robust@def*\importfiles{\cpt@testopt\fim@importfiles{}}
\robust@def*\fim@importfiles[#1]#2{%
  \usename{ltxkeys@\fim@setorlaunch keys}[KV]{importfiles}{#1}%
  % Some paths might have been suggested via \input@path
  % and/or \graphicspath. So we heed them here.
  \edef\fim@currpaths{%
    \ifdefTF\input@path\input@path{}%
    \ifdefTF\Ginput@path\Ginput@path{}%
    \fim@inputpaths
  }%
  \edef\fim@tempa{\ifmacroTF{#2}\expandcsonce\unexpanded{#2}}%
  \docommalist*\fim@tempa{%
    \def\currfile{##1}%
    \fim@stripbraces\currfile
    \def\siso@do####1{%
      \def\currpath{####1}%
      \fim@stripbraces\currpath
      \ifnum\fim@procnr=\z@pt
        \let\currfileext\fim@graphicsext
      \else
        \let\currfileext\fim@fileext
      \fi
      \edef\fileonpath{\currpath\currfile.\currfileext}%
      % Hmm, why don't I want to use \IfFileExists?
      \openin\@inputcheck\fileonpath\relax
      \ifcondTF\ifeof\@inputcheck\fi{}{%
        \typeout{File '\fileonpath' found}%
        \closein\@inputcheck
        \fim@doimport
      }%
    }%
    \s@expandarg\siso@@loop\fim@currpaths
  }%
}
\robust@def*\fim@doimport{%
  \fim@stuffbefore
  \begintoksgroup
  \ifcase\fim@procnr
    \toks@{%
      \begin{\fim@align}%
      \cptexpanded{%
        \noexpand\includegraphics
        [viewport=\fim@viewport,scale=\fim@scale,
        \ifcsemptyTF\fim@height{}{height=\fim@height,}%
        \ifcsemptyTF\fim@width{}{width=\fim@width,}%
        \iffim@clip,clip\fi]{\fileonpath}%
      }%
      \end{\fim@align}%
    }%
  \or
    \toks@{\input\fileonpath}%
  \or
    \toks@{%
      \let\fim@savpath\input@path
      \edef\input@path{{\currpath}}%
      \fiminclude\currfile
      \let\input@path\fim@savpath
    }%
  \fi
  \endtoksgroup
  \fim@stuffafter
}
\robust@def*\fiminclude#1{%
  \relax
  \ifnumcmpTF\@auxout=\@partaux{%
    \@latex@error{Command \noexpand\fiminclude cannot be nested}\@ehc
  }{%
    \edef\reserved@a{\cptzapspaces{#1}}%
    \s@expandarg\fim@include\reserved@a
  }%
}
\robust@def*\fim@include#1{%
  \xifinsetTF{.tex\relax}{#1\relax}{%
    \def\reserved@a##1.tex\@nil{%
      \def\fim@filename{##1}%
    }%
    \reserved@a#1\@nil
  }{%
    \def\fim@filename{#1}%
  }%
  \clearpage\relax
  \if@partsw
    % I don't know why LaTeX chose to do a loop here. We don't need it here:
    \xifinsetTF{,\fim@filename,}{,\fim@partlist,}%
      \@tempswatrue\@tempswafalse
  \else
    \@tempswatrue
  \fi
  \ifboolTF{@tempswa}{%
    \if@filesw
      \immediate\write\@mainaux{\string\@input{\[email protected]}}%
    \fi
    \let\@auxout\@partaux
    \if@filesw
      \immediate\openout\@partaux\[email protected]
      \immediate\write\@partaux{\relax}%
    \fi
    \@input@{\[email protected]}%
    \clearpage\relax
    \@writeckpt{\fim@filename}%
    \if@filesw
      \immediate\closeout\@partaux
    \fi
  }{%
    \deadcycles\z@pt
    \@nameuse{cp@\fim@filename}%
  }%
  \let\@auxout\@mainaux
}
\robust@def*\fimincludeonly#1{%
  \@partswtrue
  \cptexpandarg{\cptfiltermergecsv\fim@partlist}
    {\cptzapspaces{#1}}\nofilter
}
\cptonlypreamble\fimincludeonly
\makeatother

\fimincludeonly{subfile1a}
\begin{document}
\section{Test section}

\importfiles[
  processor   =includegraphics,
  viewport    =20 21 590 400,
  scale       =.25,
  inputpaths  ={D:/MyTempDoc/Path1/,D:/MyTempDoc/Path2/},
  stuffbefore =\bigskip\centering Importing \currfile\space from path \currpath\endgraf,
  stuffafter  =\endgraf\centering File import succeeded (\currfile).\endgraf
]{
  comet1
}

\importfiles[
  processor   =include,
  inputpaths  ={D:/MyTempDoc/Path1/,D:/MyTempDoc/Path2/}
]{
  subfile1a,subfile1b,subfile1c
}

\importfiles[
  processor   =\input,
  inputpaths  ={D:/MyTempDoc/Path1/,D:/MyTempDoc/Path2/}
]{
  subfile1a,subfile1b,subfile1c
}
\end{document}

这只是代码的部分列表。完整版本可以以以下方式处理独立文档文档通关独立软件包。我将把它作为一个软件包发布。

在此处输入图片描述 在此处输入图片描述

相关内容