0

I write/collect some codes (e.g., C, R or Matlab) in a subdirectory named, say, myCodes. Then I want to use VerbatimInput from usepackage{fancyvrb} to list the codes in some absolute path way. I know the graphicspath from usepackage{graphicx} can do this work for figures. How the same issue can be solved for codes? My .tex file reads

\documentclass{article}
% ================= figure================ 
%\usepackage{graphicx}
%\graphicspath{{./myFigs/}}
% ================= code================ 
\usepackage{fancyvrb}
%\codespath{{./myCodes/}} %??????

\begin{document}

\VerbatimInput[label=hello.c]{myCodes/hello.c} 
%\VerbatimInput[label=hello.c]{hello.c} %??????

\end{document} 

And my sample C code hello.c reads

#include <stdio.h>
int main(void)
{
    printf("Hello, world!\n"); 
    return 0;
}

According to Christian Hupfer, I mimic that as

    %=========================================
    \newcommand{\codepath}{myCodes}
    %-----------------------------------------
    \makeatletter
    \newcommand{\includecode}[1]{%
    \@ifundefined{codepath}{%
        \InputIfFileExists{#1}{}{}%
    }{%
        \InputIfFileExists{\codepath/#1}{}{\InputIfFileExists{#1}{}{}}%
    }
    }
    \makeatother

Then I use

  \VerbatimInput[label=hello.c]{\includecode{hello.c}}

but it still doesn't work.

  • Welcome to TeX.SX! Implementing path prefixes is something really difficult. Even graphicspath is not perfect. – TeXnician Jun 16 '18 at 10:02
  • Welcome to TeX - LaTeX! There is no default mechanism set-up for this. The graphicspath code is fairly complicated, but could be emulated. Do you need just one directory or a whole list? – Andrew Swann Jun 16 '18 at 10:03
  • Tks anyway@ TeXnician@ Andrew Swann – John Stone Jun 16 '18 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.