5

When publishing an m-file from MATLAB to LaTeX, a vertical space of 1em is inserted after each text paragraph. I frequently input these published files directly into a master document by using the standalone package, but the 1em vspace doesn't look good with the rest of the typography.

My question is then, is there a way to state in the preamble of the master document to somehow ignore the command \vspace{1em}?

mwe:

\documentclass{article}
\usepackage{graphicx}
\usepackage{color}  
\sloppy 
\definecolor{lightgray}{gray}{0.5}
\setlength{\parindent}{0pt}
\begin{document}

\begin{par}
Some text
\end{par} \vspace{1em}
\begin{verbatim}
syms x
x=solve(3*x-3==2)
\end{verbatim}
\color{lightgray} \begin{verbatim} 
x =

5/3

\end{verbatim} \color{black}
\begin{par}
Some more text
\end{par} \vspace{1em}

\end{document}

m-file:

%%
% Some text
syms x
x=solve(3*x-3==2)
%%
% Some more text
Anders
  • 117
  • 1
    First: Standard LaTeX does not define an environment par, but the command \par (but LaTeX allows more or less any command as an environment). Second: You seem to want to replace \end{par} \vspace{1em} by \end{par}. I wouldn't do that in TeX (where it is difficult) but in a preprocessor (e.g. in sed/perl/python/lua/...) – Martin Schröder Oct 09 '12 at 14:19
  • I just tried publishing a little .m file to latex and didn't get any \vspace added. What was your original .m file for that example, and which version of matlab (I use 2012b) – David Carlisle Oct 09 '12 at 15:08
  • @DavidCarlisle did you get any paragraphs? You need to use MATLAB code blocks that start with %% and then have a comment line with %. If you follow the example given in the documentation with sine_wave.m and add two lines %% code cell and % This is a code cell you will see it. – StrongBad Oct 09 '12 at 15:16
  • Exactly what Daniel says. I have written the m-code in the OP. – Anders Oct 09 '12 at 15:32
  • @DanielE.Shub are I'd used a combination of %% and % but had code in between:( I see it now – David Carlisle Oct 09 '12 at 15:36

2 Answers2

13

Another approach would be to modify the MATLAB style to produce the text you want.

The default style sheet for LaTeX mxdom2latex.xsl (in R2011a it is located in ...toolbox/matlab/codetools/private/) has the following section

<xsl:template match="p">\begin{par}
<xsl:apply-templates/><xsl:text>
\end{par} \vspace{1em}
</xsl:text>
</xsl:template>

Editing the style (or creating a new one) without the \vspace{1em} should give you what you want.

Assuming you rename the style sheet to be mylatex.xsl then in MATLAB you could publish myfile.m with the following in MATLAB

publish('myfile.m', struct('stylesheet', [pwd, filesep, 'mylatex.xsl'], 'format', 'latex'));
StrongBad
  • 20,495
  • +1 oops I just posted the same (now deleted). Only other information in my answer was that as an alternative to the command line you can specify the new xsl location in the publish configuration menu so it works with the publish button in the gui. – David Carlisle Oct 09 '12 at 16:05
  • Thanks, I never thought of that. That's also a very valid suggestion -- I might look into it. Would you happen to know where that file is located on a Mac installation? – Anders Oct 09 '12 at 16:21
  • nm, I found it:) – Anders Oct 09 '12 at 17:14
4

I guess using your text editor, sed or something like that would be an easy way to edit the source file or Daniel E. Shub's answer helps you to create the initial files without the \vspace{1em}, for any existing files you can use the following:

Within LaTeX you could just replace all \vspace{1em} with nothing by adding:

\usepackage{xstring}
\let\Oldvspace\vspace
\renewcommand{\vspace}[1]{\IfEq{#1}{1em}{}{\Oldvspace{#1}}}

If you use the figures within another environment, you could also use renewenvironment to apply this only to this environment.

jens_bo
  • 1,752
  • Hey thanks, that worked perfectly. I have no idea what it means or what "sed" is, but this works, and that's enough for me:) – Anders Oct 09 '12 at 15:05
  • sed is a stream editor (that is not used here...) It works like that: You put a file (stream) in and you get something out that is changed by certain rules. This way you could replace the "\vspace{1em}" with something else, but you would have do it outside of LaTex for all your MATLAB files. – jens_bo Oct 09 '12 at 15:21
  • An alternative would be to define \endpar to be `\vspace{-1em}. – StrongBad Oct 10 '12 at 10:01