[Tex/LaTex] Use of graphicspath in class

conditionalsdocument-classesgraphicsincludegraphics

I am using my own class MyReportClass to write a report. I have taken inspiration from this answer to redefine the \includegraphics command inside the class in order to use the example-image-a in case the input image does not exist.

In the class I also have the \graphicspath command in order to set the folder where I am supposed to have all the images that I want to use. However this command seems not to work properly, since for all \includegraphics command that I use in the main code the images are not found unless I write their entire path (that should be unnecessary). I think that this has to do with the redefinition of \includegraphics in the class, since without such redefinition the \graphicspath command works fine.

What I would like to achieve is the following:

  • \graphicspath should allow me to just write the image name when using \includegraphics in the main code (example: \includegraphics[width=0.85\textwidth]{myimage.eps});
  • the command \frontcoverpic defined in the class and used in the main code should take as input only the image name and find the image in the path given by \graphicspath (example: \frontcoverpic{mycoverpic.eps}, with mycoverpic.eps being in the folder defined by \graphicspath);
  • in the commands \makemargins and \makecoverpage defined in the class I should be able to use \includegraphics pointing at a different folder w.r.t. to the one indicated by \graphicspath (example: \includegraphics[width=0.25\textwidth]{./logos/logo.pdf} with ./logos/ being a different path w.r.t. the one given by \graphicspath);
  • in all cases described above, when the image is not found the example-image-a should be used.

The class:

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

Example of a main code:

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

Best Answer

How to define a relative folder path for graphics and how to turn on/off included figures?

At first, you don't need the section of

%-------------- Define command for missing figures ------------

There are basically two things you need to do:

1- Assign a fixed name but relative path folder(s) in your class to be used for each project you do, assuming creating relative folder(s) \logos (and perhaps additional folder \figures) under each main working folder. This is achieved by the following code in your custom class:

\graphicspath{{logos/}{figures/}}

2- Unloading your logos and figures (for example during focusing on the initial phase of writing text in the project, or to reduce repeated recompile time, etc..). In this case you can define a command, say \turnofffigures in your class that you can use later in the preamble or the body of your working main.tex:

\newcommand*{\turnofffigures}{\renewcommand{\includegraphics}[2][]{\fbox{FIGURE}}}

When you execute the above command \turnofffigures in your main.tex, all the \includegraphics images that you defined normally in your main file, will disappear and will be substituted by "FIGURE" word surrounded by frame box and underneath the caption you may have defined.

The complete code will be as follows:

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

The output with %\turnofffigures commented:

enter image description here

The output with \turnofffigures active (un-commented):

enter image description here

The folder structure:

\main\test.tex

\main\figures\apple.jpg

\main\logos\testlogo.png

Related Question