I am attempting to document my MATLAB programming code in LaTeX. For
this, I use the mcode
package, which defines the MATLAB lstlisting
environment. With the commands
\newcommand{\mfileMain}{main.m}
\newcommand{\mfileMainFirstNumber}{\mfileMainFirstLine}
\newcommand{\mfileMainFirstLine}{26}
\newcommand{\mfileMainLastLine}{33}
\lstinputlisting[firstnumber=\mfileMainFirstNumber,
firstline=\mfileMainFirstLine,
lastline=\mfileMainLastLine]{\mfileMain}
I set the firstnumber
, firstline
and lastline
parameters and use
them to import a piece of MATLAB code into the LaTeX document with
\lstinputlisting
. However, when I add some code before the
\mfileMainFirstLine
, in this case 26, the code is shifted and the
documentation is ruined. To fix this, I would have to update the line
numbers.
I am trying to fix this issue by adding 'tracers' to my MATLAB program. I use the commands
filename=['lstinputlisting_line_numbers_' mfilename '.txt'];
fileID=fopen(filename,'w');
to create a text file in which I store the line numbers for which the code is documented using the command
dbstack_eval=dbstack;fprintf(fileID,[num2str(dbstack_eval.line+1) '\n']);
. This results in a text file lstinputlisting_line_numbers_.txt
with
content for instance like
53
62
75
91
in case I want to document four parts of the MATLAB program main.m
.
What I want to do from here is simply use this line number data in the
\lstinputlisting
commands. Perhaps it is possible to store the line
numbers in the \newcommands{}{}
, or perhaps there is a better, more
elegant way of doing it. In this way, the line numbers would update
automatically in case I add new lines of code. Consequently, my LaTeX
document would still be okay, saving me a lot of time not having to
update the line numbers myself.
A minimum working example could look like the following. The LaTeX file
main.tex
would have the content
\documentclass[a4paper]{article}
\usepackage[framed,numbered]{mcode}
% Data for the 'lstinputlisting' environments.
\newcommand{\mfileMain}{main.m}
\newcommand{\mfileMainFirstNumberA}{\mfileMainFirstLineA}
\newcommand{\mfileMainFirstLineA}{WHAT SHOULD I PUT HERE?}
\newcommand{\mfileMainLastLineA}{WHAT SHOULD I PUT HERE?}
\newcommand{\mfileMainFirstNumberB}{\mfileMainFirstLineB}
\newcommand{\mfileMainFirstLineB}{WHAT SHOULD I PUT HERE?}
\newcommand{\mfileMainLastLineB}{WHAT SHOULD I PUT HERE?}
\begin{document}
In the code
\lstinputlisting[firstnumber=\mfileMainFirstNumberA,
firstline=\mfileMainFirstLineA,
lastline=\mfileMainLastLineA]{\mfileMain}
we calculate the ....
And in the code
\lstinputlisting[firstnumber=\mfileMainFirstNumberB,
firstline=\mfileMainFirstLineB,
lastline=\mfileMainLastLineB]{\mfileMain}
we calculate the.....
\end{document}
. The MATLAB file main.m
would have the content
%% Line number output.
filename=['lstinputlisting_line_numbers_' mfilename '.txt'];
fileID=fopen(filename,'w');
dbstack_eval=dbstack;fprintf(fileID,[num2str(dbstack_eval.line+1) '\n']);
%% Input to be specified by user.
alpha=10; % PDE parameter.
x_s=0; % Starting coordinate.
x_e=1; % End coordinate.
N_n=1e2+1; % (N)umber of (n)odes.
N_ip=2; % (N)umber of (i)ntegration (p)oints.
dbstack_eval=dbstack;fprintf(fileID,[num2str(dbstack_eval.line+1) '\n']);
fclose(fileID); % Close the file 'fileID'.
matlab-prettifier
package; it improves uponmcode
in many ways. See this answer, for instance. – jub0bs Sep 8 '14 at 9:20listings
manual. – jub0bs Sep 8 '14 at 9:25matlab-prettifier
package looks great. But it does not solve the current problem, or does it? And concerning Section 5.7 in the listings manual....I don't really understand it :(... Perhaps it solves the issue. Could you perhaps take my minimum working example and adapt it accordingly in an answer here? Then everybody can test it. Thanks a lot! – Adriaan Sep 8 '14 at 9:44