24

I work a lot with MATLAB, and I want to enable the reader of my paper to easily reproduce my computations. I also want the pdf document to be self-contained, i.e., no hyperlinks to a webpage on which the code is available. Is there a good way to accomplish this in LaTeX?

Unfortunately, I find that authors frequently print their computer code in the paper, and the code might be broken over multiple pages, thereby making copy-and-paste a tedious task.

I envision two alternatives:

  1. In the pdf, there is a link that when clicked, prompts the user to save an m file in a directory of his choosing. I do not want the m file to reside in a local network drive (as in this question), but rather, I want the m file code to somehow reside under the hood of the pdf (much like how the pdf includes graphics, say).

  2. In the pdf, there is a text box with a scroll bar which contains my MATLAB code. If interested, the reader can easily copy and paste as desired.

I would be very happy to learn how to implement either of these (or other alternatives) in LaTeX. Thanks for the help!

3
  • 2
    You can attach the .m file to your pdf so that readers can get a copy. – user11232 May 8 '15 at 12:32
  • Welcome to TeX.SX! You could attach files to a pdf document even with LaTeX, e.g. the source code of your matlab files – user31729 May 8 '15 at 12:32
  • 2
    A few people have suggested attaching the .m file, which is great when it works. I'd be surprised if this was possible for a journal paper in very many journals (this is one of those times when I'd be delighted to be proved wrong) though some allow auxiliary files (e.g. video data). I'm not sure exactly what pdf readers support attachments, but bear in mind that linux is widely used in some scientific fields, and there's no longer an adobe reader for linux. – Chris H May 8 '15 at 19:05
20

The PDF format includes file attachments, which means other files can be attached much like in emails.

One package that can do this is the attachfile package. This package inserts a link to the attached file in the document text.

My preferred package is navigator. Navigator simply attaches the file; nothing appears in the document. The file is accessed using the attachments window in the PDF viewer.

The navigator command for attaching a file is

\embeddedfile[<description>]{<object name>}[<alternate filename>]{<file>}

where the file <file> is attached to the document under the name <file> (or <alternate filename> if present) and with the description <description>. The <object name> is an internal name for the attached file and is invisible to the reader of the document.

0
10

This uses the attachfile2 package (the attached file is just the ordinary hello world example in C (ok, it's not Matlab ;-))

\documentclass{article}

\usepackage[pdfversion=1.7]{hyperref}
\usepackage{attachfile2}



\begin{document}
This is the famous Hello World example: \attachfile[color=0 1 0,description={The famous Hello World example}]{helloworld.c}

\end{document}

%%%

% Example helloworld.c

#include <stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}

enter image description here

Edit The attachfile2 is a follow-up package of attachfile. There are some keys to the \attachfile command, which are described in the documentation of attachfile.

1
  • Being attachfile2 a follow-up from attachfile, the latter has had its last update in 2015; is there any consequence? Anything new in attachfile that it's not in attachfile2? – Manuel May 13 '15 at 13:06
3

Regarding the second alternative, check out listings package.

3
  • 1
    Hi and welcome, can you add a little explanation to your answer? Right now, it really is just a pointer to a package, more a comment than an answer. – Johannes_B May 8 '15 at 14:04
  • Yes, it would have been a comment if I could post comments ... – Tony May 8 '15 at 14:07
  • You will soon have enough reputation to fully join in the commenting fun. ;-) – Johannes_B May 8 '15 at 14:08
3

The listings package allows the inclusion (and syntactic highlighting) of code.

The code either comes from a file, like

\lstinputlisting[frame=single,language=C++,numbers=left]{myfile.cc}

Here, decorated with a frame and line numbering. Or is directly written in the LaTeX source :

\begin{lstlisting}
    cout << "hello, world"
         << endl;
\end{lstlisting}

The Matlab language is supported (see also Using listing for displaying Matlab code).

1
  • 2
    Your answer is exactly what the OP tries to avoid. – Ruben May 13 '15 at 10:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.