如何在 latex 中处理子文件图片

如何在 latex 中处理子文件图片

现在我正在使用子文件来编译独立文档,我想在子文档中插入一张图片,如下所示:

\documentclass[../../../dolphin-book-2020.tex]{subfiles}

\begin{document}

\subsection{GitHub Slow}

balabalabala.......


\begin{figure}[htbp]
    \centering
    \includegraphics[scale=0.2]{githubspeedup}
    \caption{GitHub Speed Up}
    \label{fig:githubspeedup}
\end{figure}


\end{document}

当我在 macOS Catalina 10.15 中使用此命令进行编译时:

 /Library/TeX/texbin/latexmk -pdfxe -pvc -xelatex -interaction=nonstopmode ./github-slow.tex

显示此错误:

(/usr/local/texlive/2020/texmf-dist/tex/latex/listings/lstlang1.sty)
(/usr/local/texlive/2020/texmf-dist/tex/latex/listings/lstlang1.sty)

Package fontspec Warning: Font "FandolFang-Regular" does not contain requested
(fontspec)                Script "CJK".


! LaTeX Error: File `githubspeedup' not found.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...

l.31 ^^I\includegraphics[scale=0.2]{githubspeedup}

[1] (./github-slow.aux)

LaTeX Font Warning: Some font shapes were not available, defaults substituted.


LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

 )
(see the transcript file for additional information)
Output written on github-slow.xdv (1 page, 28900 bytes).
Transcript written on github-slow.log.
=== TeX engine is 'XeTeX'
Latexmk: Index file 'github-slow.idx' was written
Latexmk: Missing input file: 'githubspeedup' from line
  '! LaTeX Error: File `githubspeedup' not found.'
Latexmk: References changed.
Latexmk: Log file says output to 'github-slow.xdv'
Latexmk: Errors, so I did not complete making targets
Latexmk: Failure to make the files correctly
    ==> You will need to change a source file before I do another run <==
Collected error summary (may duplicate other messages):
  xelatex: Command for 'xelatex' gave return code 1
      Refer to 'github-slow.log' for details

=== Watching for updated files. Use ctrl/C to stop ...
^CLatexmk: User typed ctrl/C or ctrl/break.  I'll finish.
Collected error summary (may duplicate other messages):
  xelatex: Command for 'xelatex' gave return code 1
      Refer to 'github-slow.log' for details
Latexmk: Use the -f option to force complete processing,
 unless error was exceeding maximum runs, or warnings treated as errors.

我该怎么做才能让它工作?我已经在主文件中添加了图片路径,dolphin-book-2020.tex如下所示(所有图片存储在文件夹中,没有图片存储在子文件夹中):

\graphicspath{{./Pictures/}}

这是文件结构:

在此处输入图片描述

我曾尝试dolphin-book-2020.tex像这样定义图片路径:

\graphicspath{{../../../Pictures/}{../../Pictures/}{../Pictures/}{./Pictures/}} 

这是文件夹嵌套结构:

在此处输入图片描述

我也尝试写出图片的所有可能路径:

\graphicspath{
    {Pictures/}
    {../../../Pictures/}
    {../../Pictures/}
    {image/}
    {/Users/dolphin/Documents/GitHub/dolphin-book-2020/Pictures}
}

仍然找不到文件。

答案1

您需要在以下位置添加图像的搜索路径主要文件

吨

假如说:

(1)dolphin-book-2020.tex在目录中BOOK

(2)各章内容如下BOOK/chapters

(3)图像位于 BOOK/chapters/images 且子目录images包含githubspeedup.jpg

(4)子部分位于BOOK/chapters/subsections且该子目录包含GitHubSlow.tex

你会得到

A

编译 dolphin-book-2020.texGitHubSlow.tex

从主文件和子文件到图像的搜索路径被添加为

\graphicspath{{./chapters/images/}}} % path to image githubspeedup.jpg

在主文件中 dolphin-book-2020.tex<<<<<<<<

可以添加多个搜索路径,如下所示

\graphicspath{{<path1>}{<path2>}{<path3>}}}

这是dolphin-book-2020.tex

 %% dolphin-book-2020.tex in its own directory BOOK

\documentclass[12pt,a4paper]{book}

\usepackage{graphicx}

\usepackage{subfiles} 

\graphicspath{{./chapters/images/}} %  path from here to image githubspeedup.jpg   <<<<<<<<<<<<<<< added

\begin{document}
    
\subfile{./chapters/subsections/GitHubSlow}  % subsection file in  BOOK/chapters/subsections subdirectory
    
\end{document}

这是GitHubSlow.tex在 subsections 子目录中

% GitHubSlow.tex in BOOK/chapters/subsections

\documentclass[../../dolphin-book-2020.tex]{subfiles}

\begin{document}
    
    \subsection{GitHub Slow}
    
    balabalabala....... 
    
    \begin{figure}[htbp]
        \centering
        \includegraphics[scale=0.2]{githubspeedup}
        \caption{GitHub Speed Up}
        \label{fig:githubspeedup}
    \end{figure}    
    
\end{document}

有关的:无法使用子文件和 XeLaTeX 加载图片或 PDF 文件

答案2

在子文件上添加图片路径如下:

\documentclass[../../../dolphin-book-2020.tex]{subfiles}
\usepackage{graphics} % add package
\graphicspath{
    {../../../Pictures/}
} % add picture path

\begin{document}


\begin{figure}[htbp]
    \centering
    \includegraphics[scale=0.2]{codesign}
    \caption{fastlane}
    \label{fig:codesign}
\end{figure}

\end{document}

相关内容