I'm looking for a way to include source code in my thesis' appendix.

Any chance to add source code by pointing to a directory and adding the java file recursively going through the packages?

I do and did already use minted and listings. Maybe I have missed a feature of these packages. But since the java source code might change, I would like to avoid copy&paste which increase the chance to miss something. Favourable it's done automatically.

  • 2
    I am a big fan of listings. You can use \lstinputlisting to include a single source file, however, this won't recursively search through a directory for you. On the other hand, I doubt that you'd want to include hundreds of files this way but even if you did it wouldn't take you long to throw together the tex code for this using a reasonable editor and a regular expression. Of course, it wouldn't automatically update...
    – user30471
    Sep 19, 2016 at 22:57
  • ...the list of files
    – user30471
    Sep 19, 2016 at 23:25
  • Have you seen List all files from directory base on regex with extension. You could filter by .*\string\.java. Jan 22, 2018 at 19:47

2 Answers 2


To expand on my comment, you could always do something like this:


\foreach \java in  {hello, hello} {
        \caption{Source code for \textsf{\java.java}}


with output:

enter image description here

This does not recursively search for files but it will update when the source files change and it does minimize what you have to type by only requiring you to add the file names to the loop.

The MWE is more for proof of concept as you'd probably want to tweak the formatting and layout of the code. Personally, I'd never use figure, or floats in general, as I like text/environments to appear where I type them:). I just checked and, as I suspected, using figure causes problems when the source file is too long to fit on one page.

  • Awesome, ok it's not going recursively through the path but I don't have to add the path for every single file in a package. :) Thanks.
    – Thomas
    Sep 19, 2016 at 23:26
  • @ThomasZuberbühler It looks like TeX doesn't have a native way to discover every file in a directory. You could use a quick program of your own to write out a file with that information (or something like \write18{ls ...}, which would be equivalent).
    – Teepeemm
    Sep 20, 2016 at 13:56
  • @Andrew I write something like this. but doesn't work.
    – alhelal
    Sep 26, 2017 at 4:38
  • @BandaMuhammadAlHelal Your question is not directly related to this post which was about including many files in a uniform way. Is is also not clear what your problem is. I suggest that you ask a quetsion in which you include your full code and your error message and a description of the problem. The problem is almost certainly related to your paths but without knowing what your error message is it is hard to say.
    – user30471
    Sep 26, 2017 at 7:24
  • @Andrew the problem is solved. when \lstinputlisting used there is no need of \begin{lstlisting} and \end{lstlisting}.
    – alhelal
    Sep 26, 2017 at 9:56

It is easy to include single files using minted. As documented in the package documentation.


Use Andrew's answer to include multiple source files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .