我正在用我自己的班级MyReportClass
写一份报告。我从中获得了灵感这个答案重新定义\includegraphics
类内部的命令,以便example-image-a
在输入图像不存在的情况下使用。
在类中,我还有一个\graphicspath
命令,用于设置我应该在其中存放所有要使用的图像的文件夹。但是,此命令似乎无法正常工作,因为对于我在主代码中使用的所有命令,除非我写入图像的完整路径(这应该是不必要的),否则找不到图像。我认为这与类中\includegraphics
的重新定义有关,因为如果没有这样的重新定义,命令就可以正常工作。\includegraphics
\graphicspath
我想要实现的目标如下:
\graphicspath
\includegraphics
应该允许我在主代码中使用时只写图像名称(例如\includegraphics[width=0.85\textwidth]{myimage.eps}
:);- 类中定义并在主代码中使用的命令
\frontcoverpic
应该只将图像名称作为输入,并在给出的路径中查找图像\graphicspath
(例如:\frontcoverpic{mycoverpic.eps}
,位于mycoverpic.eps
定义的文件夹中\graphicspath
); - 在命令中
\makemargins
并\makecoverpage
在类中定义,我应该能够使用\includegraphics
指向与所指示的文件夹不同的文件夹\graphicspath
(例如:\includegraphics[width=0.25\textwidth]{./logos/logo.pdf}
与./logos/
给出的路径不同的路径\graphicspath
); - 在上述所有情况下,当找不到图像时,
example-image-a
应使用。
班上:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{MyReportClass}
\LoadClass[a4paper,12pt]{article} % Class of the document
%--------------------- Packages ------------------------
\RequirePackage[english]{babel} % Langueage of the document
\RequirePackage[utf8]{inputenc} % Special characters
\RequirePackage[section]{placeins} % For format of sections
\RequirePackage[T1]{fontenc} % Letters not included in UTF-8
\RequirePackage{mathtools} % Equations and mathematic symbols
\RequirePackage{siunitx} % Scientific notation
\RequirePackage{float} % Images formatting
\RequirePackage{graphicx} % Insert figures
\RequirePackage{tikz}
\RequirePackage[justification=centering]{caption} % Centred legends
\RequirePackage{subcaption}
\RequirePackage{wallpaper}
\RequirePackage{nomencl}
\RequirePackage{fancyhdr}
\RequirePackage{url}
\RequirePackage[pdfencoding=auto,psdextra]{hyperref} % Legends in sub-figures
\RequirePackage{amsmath,bm,amssymb}
\RequirePackage{amsfonts}
\RequirePackage{bookmark} % Faster updated bookmarks
\RequirePackage{textcomp}
% Package for nice tables
\RequirePackage{booktabs,makecell}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
\sisetup{
% output-decimal-marker = {,},
detect-weight,
mode = text
}
\newcommand*{\specialcell}[2][b]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
\newcommand*{\specialcellbold}[2][b]{%
\bfseries
\sisetup{text-rm = \bfseries}%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
\newcommand*{\leftspecialcell}[2][b]{%
\begin{tabular}[#1]{@{}l@{}}#2\end{tabular}%
}
%Package for labelling matrix rows and columns
\RequirePackage{blkarray}
% Package to adjust labelled matrices
\RequirePackage{makebox,bigstrut,relsize}
% Page configuration
\RequirePackage{geometry}
\geometry{a4paper,hscale=0.75,vscale=0.8}
%Package to modify chapter header
%\RequirePackage{titlesec}
%Color for sections head
\RequirePackage{color}
\definecolor{tdblue}{RGB}{0,174,239}
\RequirePackage{sectsty}
\sectionfont{\color{tdblue}}
\subsectionfont{\color{tdblue}}
\subsubsectionfont{\color{tdblue}}
%MATLAB command
\RequirePackage{xspace}
\newcommand{\MATLAB}{\textsc{Matlab}\xspace}
%Typesetting a MATLAB code
\RequirePackage[T1]{fontenc}
\RequirePackage{bigfoot} % to allow verbatim in footnote
\RequirePackage[numbered,framed]{matlab-prettifier}
\RequirePackage{filecontents}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"
\lstset{
style = Matlab-editor,
basicstyle = \mlttfamily,
escapechar = ",
mlshowsectionrules = true,
}
% Per-section figure, equation, table and lstlisting numbering
\RequirePackage{chngcntr}
\counterwithin{figure}{section}
\counterwithin{equation}{section}
\counterwithin{table}{section}
\AtBeginDocument{\counterwithin{lstlisting}{section}}
% Package for references with numbers
\bibliographystyle{apsrev4-1}
\RequirePackage[numbers]{natbib}
\setcitestyle{numbers}
% Set graphics path
\graphicspath{{./figures/}}
%-------------- Define command for missing figures ------------
\let\StandardIncludeGraphics\includegraphics%
\renewcommand{\includegraphics}[2][]{%
\IfFileExists{#2}{%
\StandardIncludeGraphics[#1]{#2}%
}{%
\StandardIncludeGraphics[#1]{example-image-a}}%
}% End of command
%-------------------- Report information ----------------------
\newcommand{\coursecode}[1]{\renewcommand{\coursecode}{#1}}
\newcommand{\coursename}[1]{\renewcommand{\coursename}{#1}}
\newcommand{\instructorname}[1]{\renewcommand{\instructorname}{#1}}
\newcommand{\reporttitle}[1]{\renewcommand{\reporttitle}{#1}}
\newcommand{\reportauthor}[3]{\renewcommand{\reportauthor}{#1 {\scshape #2} \dots\dots {\bfseries #3}}}
\newcommand{\frontcoverpic}[1]{\renewcommand{\frontcoverpic}{\includegraphics[width=0.5\textwidth]{#1}}}
\newcommand{\makemargins}{
\makenomenclature
\pagestyle{fancy}
\fancyheadoffset{1cm}
\setlength{\headheight}{2cm}
\lhead{\includegraphics[width=0.25\textwidth]{./logos/logo.pdf}} % Set image at top of the page
\rhead{\nouppercase{\leftmark}}
\rfoot{\thepage}
\cfoot{\textbf{\reporttitle}}
\lfoot{\coursecode}
}
\newcommand{\makecoverpage}{
\begin{titlepage}
\centering % Center the content
\includegraphics[width=0.4\textwidth]{./logos/logo_blue.pdf}\par\vspace{1cm} % Insert logo
{\scshape\LARGE Delft University of Technology \par} % Name of the university
\vspace{1cm} % Vertical space of 1.5 cm
{\scshape\Large \coursename \\ \large \coursecode \\ \normalsize \instructorname \par}
\vspace{0.5cm} % Vertical space of 1 cm
\rule{\linewidth}{0.2 mm} \\[0.4 cm]
{\huge\bfseries \reporttitle \par} \
\rule{\linewidth}{0.2 mm} \\[1 cm]
\vspace{0.5cm}
\frontcoverpic\\[1.5 cm]
\normalsize \reportauthor\\
\vfill
{\large \today\par} % Visualization of the date
\end{titlepage}
}
\newcommand{\initialise}{
\makemargins
\makecoverpage
\tableofcontents
\newpage
}
% Define newcommand for norm
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}
主代码示例:
\documentclass{MyReportClass}
\begin{document}
\reporttitle{My Title}
\coursecode{1234}
\coursename{My Course}
\instructorname{Instructor Name}
\reportauthor{Name}{Surname}{1234}
\frontcoverpic{mycoverpic.eps}
\initialise
\section{A section}
\begin{figure}[h]
\centering
\includegraphics[width=0.85\textwidth]{myimage.eps}
\caption{My image}
\end{figure}
\end{document}
答案1
如何定义图形的相对文件夹路径以及如何打开/关闭包含的图形?
首先,你不需要
%-------------- Define command for missing figures ------------
基本上有两件事你需要做:
1- 在您的类中分配一个固定名称但相对路径的文件夹,用于您执行的每个项目,假设在每个工作文件夹下创建相对文件夹\logos
(可能还有附加文件夹\figures
)main
。这可以通过自定义类中的以下代码实现:
\graphicspath{{logos/}{figures/}}
2- 卸载您的徽标和图形(例如,在专注于项目中编写文本的初始阶段,或减少重复重新编译的时间等)。在这种情况下,您可以\turnofffigures
在类中定义一个命令,您可以在工作序言或正文中稍后使用它main.tex
:
\newcommand*{\turnofffigures}{\renewcommand{\includegraphics}[2][]{\fbox{FIGURE}}}
\turnofffigures
当你在 main.tex 中执行上述命令时,\includegraphics
你在主文件中正常定义的所有图像都将消失,并被框起来并位于你可能定义的标题下方的“FIGURE”字样所取代。
完整代码如下:
\documentclass[12pt]{article}
%
\usepackage{graphicx} % use \RequirePackage when including in custom class file
\graphicspath{{figures/}{logos/}} % relative path of figures and logos
\newcommand*{\turnofffigures}{\renewcommand{\includegraphics}[2][]{\fbox{FIGURE}}} % command to turn off figures when no need to load them
%
%
\begin{document}
Test file
%
\turnofffigures % comment and uncomment to hide/show images
%
Test loading image from logos folder
%
\begin{figure}[h]
\centering
\includegraphics[width=0.3\linewidth]{testlogo.png} % image name here is defined without path and loaded from main\logos folder
\caption{Logo picture loaded from logos folder.} \label{fig:log}
\end{figure}
%
%
Test loading image from figures folder
\begin{figure}[h]
\centering
\includegraphics[width=0.3\linewidth]{apple.jpg} % image name here is defined without path and loaded from main\figures folder
\caption{Apple picture loaded from figures folder.} \label{fig:app}
\end{figure}
%
\end{document}
带有注释的输出%\turnofffigures
:
活动输出\turnofffigures
(未注释):
文件夹结构:
\main\test.tex
\main\figures\apple.jpg
\main\logos\testlogo.png
答案2
正如 alephzero 在问题评论中所指出的,\ifFileExists
不了解\graphicspath
,因此它不会在所需的路径中查找。解决方案是在命令中明确指示\ifFileExists
在 指示的文件夹中查找\graphicspath
。
为了完成问题的第三点,我实现了一个嵌套的\ifFileExists
。基本上,宏将首先在给出的路径中查找\graphicspath
,如果没有找到任何内容,它将查找作为输入提供的整个表达式(因此在的情况下./logos/logo.pdf
,宏将直接查看提供的路径)。只有在两种情况下都没有找到任何内容时,才会example-image-a
使用。
现在一切正常,但是我不明白\StandardIncludeGraphics
当图像不在提供的路径中时命令如何设法找到图像\graphicspath
。
修改后的类:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{MyReportClass}
\LoadClass[a4paper,12pt]{article} % Class of the document
%--------------------- Packages ------------------------
\RequirePackage[english]{babel} % Langueage of the document
\RequirePackage[utf8]{inputenc} % Special characters
\RequirePackage[section]{placeins} % For format of sections
\RequirePackage[T1]{fontenc} % Letters not included in UTF-8
\RequirePackage{mathtools} % Equations and mathematic symbols
\RequirePackage{siunitx} % Scientific notation
\RequirePackage{float} % Images formatting
\RequirePackage{graphicx} % Insert figures
\RequirePackage{tikz}
\RequirePackage[justification=centering]{caption} % Centred legends
\RequirePackage{subcaption}
\RequirePackage{wallpaper}
\RequirePackage{nomencl}
\RequirePackage{fancyhdr}
\RequirePackage{url}
\RequirePackage[pdfencoding=auto,psdextra]{hyperref} % Legends in sub-figures
\RequirePackage{amsmath,bm,amssymb}
\RequirePackage{amsfonts}
\RequirePackage{bookmark} % Faster updated bookmarks
\RequirePackage{textcomp}
% Package for nice tables
\RequirePackage{booktabs,makecell}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}
\sisetup{
% output-decimal-marker = {,},
detect-weight,
mode = text
}
\newcommand*{\specialcell}[2][b]{%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
\newcommand*{\specialcellbold}[2][b]{%
\bfseries
\sisetup{text-rm = \bfseries}%
\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}%
}
\newcommand*{\leftspecialcell}[2][b]{%
\begin{tabular}[#1]{@{}l@{}}#2\end{tabular}%
}
%Package for labelling matrix rows and columns
\RequirePackage{blkarray}
% Package to adjust labelled matrices
\RequirePackage{makebox,bigstrut,relsize}
% Page configuration
\RequirePackage{geometry}
\geometry{a4paper,hscale=0.75,vscale=0.8}
%Package to modify chapter header
%\RequirePackage{titlesec}
%Color for sections head
\RequirePackage{color}
\definecolor{tdblue}{RGB}{0,174,239}
\RequirePackage{sectsty}
\sectionfont{\color{tdblue}}
\subsectionfont{\color{tdblue}}
\subsubsectionfont{\color{tdblue}}
%MATLAB command
\RequirePackage{xspace}
\newcommand{\MATLAB}{\textsc{Matlab}\xspace}
%Typesetting a MATLAB code
\RequirePackage[T1]{fontenc}
\RequirePackage{bigfoot} % to allow verbatim in footnote
\RequirePackage[numbered,framed]{matlab-prettifier}
\RequirePackage{filecontents}
\let\ph\mlplaceholder % shorter macro
\lstMakeShortInline"
\lstset{
style = Matlab-editor,
basicstyle = \mlttfamily,
escapechar = ",
mlshowsectionrules = true,
}
% Per-section figure, equation, table and lstlisting numbering
\RequirePackage{chngcntr}
\counterwithin{figure}{section}
\counterwithin{equation}{section}
\counterwithin{table}{section}
\AtBeginDocument{\counterwithin{lstlisting}{section}}
% Package for references with numbers
\bibliographystyle{apsrev4-1}
\RequirePackage[numbers]{natbib}
\setcitestyle{numbers}
% Set graphics path
\graphicspath{{./figures/}}
%-------------- Define command for missing figures ------------
\let\StandardIncludeGraphics\includegraphics%
\renewcommand{\includegraphics}[2][]{%
\IfFileExists{./figures/#2}{%
\StandardIncludeGraphics[#1]{#2}%
}{%
\IfFileExists{#2}{%
\StandardIncludeGraphics[#1]{#2}%
}{%
\StandardIncludeGraphics[#1]{example-image-a}}}%
}% End of command
%-------------------- Report information ----------------------
\newcommand{\coursecode}[1]{\renewcommand{\coursecode}{#1}}
\newcommand{\coursename}[1]{\renewcommand{\coursename}{#1}}
\newcommand{\instructorname}[1]{\renewcommand{\instructorname}{#1}}
\newcommand{\reporttitle}[1]{\renewcommand{\reporttitle}{#1}}
\newcommand{\reportauthor}[3]{\renewcommand{\reportauthor}{#1 {\scshape #2} \dots\dots {\bfseries #3}}}
\newcommand{\frontcoverpic}[1]{\renewcommand{\frontcoverpic}{\includegraphics[width=0.5\textwidth]{#1}}}
\newcommand{\makemargins}{
\makenomenclature
\pagestyle{fancy}
\fancyheadoffset{1cm}
\setlength{\headheight}{2cm}
\lhead{\includegraphics[width=0.25\textwidth]{./logos/logo.pdf}} % Set image at top of the page
\rhead{\nouppercase{\leftmark}}
\rfoot{\thepage}
\cfoot{\textbf{\reporttitle}}
\lfoot{\coursecode}
}
\newcommand{\makecoverpage}{
\begin{titlepage}
\centering % Center the content
\includegraphics[width=0.4\textwidth]{./logos/logo_blue.pdf}\par\vspace{1cm} % Insert logo
{\scshape\LARGE Delft University of Technology \par} % Name of the university
\vspace{1cm} % Vertical space of 1.5 cm
{\scshape\Large \coursename \\ \large \coursecode \\ \normalsize \instructorname \par}
\vspace{0.5cm} % Vertical space of 1 cm
\rule{\linewidth}{0.2 mm} \\[0.4 cm]
{\huge\bfseries \reporttitle \par} \
\rule{\linewidth}{0.2 mm} \\[1 cm]
\vspace{0.5cm}
\frontcoverpic\\[1.5 cm]
\normalsize \reportauthor\\
\vfill
{\large \today\par} % Visualization of the date
\end{titlepage}
}
\newcommand{\initialise}{
\makemargins
\makecoverpage
\tableofcontents
\newpage
}
% Define newcommand for norm
\newcommand{\norm}[1]{\left\lVert#1\right\rVert}