TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}?



Some text
\end{par} \vspace{1em}
syms x
\color{lightgray} \begin{verbatim} 
x =


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



% Some text
syms x
% Some more text
share|improve this question
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 9 '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 9 '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 9 '12 at 15:16
Exactly what Daniel says. I have written the m-code in the OP. – Anders Oct 9 '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 9 '12 at 15:36
up vote 4 down vote accepted

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:


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

share|improve this answer
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 9 '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 9 '12 at 15:21
An alternative would be to define \endpar to be `\vspace{-1em}. – StrongBad Oct 10 '12 at 10:01

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}
\end{par} \vspace{1em}

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'));
share|improve this answer
+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 9 '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 9 '12 at 16:21
nm, I found it:) – Anders Oct 9 '12 at 17:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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