2

I am writing my reports which need to reference complete *.java files with latex. I am using the Texlipse Eclipse plugin to do the editing.

Right now I am referencing the complete path to each *.java file within the *.tex file like so:

\subsection*{Foo-class}
\lstinputlisting{/home/pathtoexlipse/workspace/Foo.java}

Since we are collaborating on the source files and the documentation with help of a VCS, it would be nice to have a means to reference a relative path within the latex document.

The folder structure within the project-file always looks like this:

Project_Folder/
|src/
|---Foo.java
|---Bar.java
|latex_documentation/
|--mydocument.tex

But since editing takes place under different systems, where the project-files are located at different absolute path on the filesystem, it would be nice to have a possibility to just reference the relative path when invoking \lstinputlisting.

Also, it would be great, if it would be possible to automatically parse a directory for all *.java files, and automatically include them within the \lstinputlisting.

Is there a means in latex to achieve these desired features?

jottr
  • 374
  • Please wait, I am preparing the answer. :-) – Display Name Dec 05 '10 at 02:45
  • You can use \write18 to execute commands on the system, but this is generally considered unsafe and thus disabled by default. You can enable it by passing arguments like -shell-escape to pdflatex. I think it's better to run something outside of latex to generate the file listing that you can parse in TeX. – TH. Dec 05 '10 at 06:41
  • @elements, @TH, I have written the application in c#, please see the last part of my answer. Use Mono .NET runtime to compile and run the application. – Display Name Dec 05 '10 at 14:18
  • I don’t understand the problem. TeX works just fine with relative paths. Just use something like this: \lstinputlisting{Foo.java}. – Konrad Rudolph Dec 05 '10 at 15:07
  • @Konrad, the topic starter @elementz wants to include any java source code in a directory under the directory in which the main TeX input file resides automatically and using relative paths. – Display Name Dec 05 '10 at 15:16
  • @xport: granted, that was only the second question. In my understanding, the first (and main) question was how to work with relative paths. – Konrad Rudolph Dec 05 '10 at 19:28
  • Is this related to: http://tex.stackexchange.com/q/4602/86 ? – Andrew Stacey Dec 06 '10 at 08:53
  • I figured, as @Konrad already mentioned, that a relative path can indeed be simply referenced like so: \lstinputlisting{../src/Foo.java}

    Still, I would like, as I already mentioned, to be able to parse any src/-directory, and automatically include all *.java-files present in that directory, without explicitly having to reference to every single file one by one.

    – jottr Dec 06 '10 at 11:09

1 Answers1

3

Create a batch file

If you are working on a non-Windows computer, please adapt the following batch.

rem batch.bat takes 2 mandatory arguments.
rem %1 represents your java code directory path.
rem %2 represents main TeX input file name (aka jobname).
set curdir=%CD%
cd %1
dir /b *.java > %curdir%\%2.xport

For the sake of simplicity, save this batch in the same directory in which your main TeX input file exists. In your case, save it in latex_documentation directory.

If you want to reuse this batch for other projects, you should set the system variable PATH to a directory in which this batch exists.

Main input file

\documentclass{article}
\usepackage{listings,xcolor}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage[colorlinks]{hyperref}


{
\catcode`\^0
\catcode`\\12
^gdef^dirsep{\}
}

\lstset
{
    language=Java,
    frame=single,
    breaklines=true,
    basicstyle=\small\tt,
    keywordstyle=\color{blue}\sf,
    identifierstyle=\color{magenta},
    commentstyle=\color{cyan},
    backgroundcolor=\color{yellow!5}
}

\newcount\TotalFiles
\newread\myfile
\newcommand\ImportAllSourceCodes
{%
    \immediate\write18{batch ..\dirsep src \jobname}% edit . to any directory in which the code exist.
    %\newread\myfile% should not be in a macro
    \immediate\openin\myfile=\jobname.xport\relax
    %\newcount\TotalFiles% should not be in a macro
    \TotalFiles=0
    \loop
        \read\myfile to \mydata
        \unless\ifeof\myfile
            \section\mydata
            \lstinputlisting[caption={\href{\mydata}{\mydata}}]{"../src/\mydata"}\newpage% also edit . here.
            \advance \TotalFiles by 1
    \repeat
    \immediate\closein\myfile
}


\begin{document}
\ImportAllSourceCodes
There are \the\TotalFiles\ files in total.
\end{document}

DON'T forget to pass -enable-write18 to TeX compiler!

The trick, to escape \, is taken from SamB's answer

{
\catcode`\^0
\catcode`\\12
^gdef^dirsep{\}
}
Display Name
  • 46,933