| Overcoming some flaws with graphics created using matlab2tikz |
|
|
|
| LaTeX - Graphics, Figures & Tables |
| Written by Frits Wenneker |
| Wednesday, 20 June 2012 21:58 |
|
Matlab is a widely used numerical computation environment. It is easy to make plots in Matlab, which can be exported to vector format (PDF/EPS) for implementation in LaTeX. The problem is that the default Matlab plots do not fit very well in the style of LaTeX, due to the different font and font size of the text in the Matlab plots. My favorite way of creating Matlab figures with LaTeX fonts is by making use of the - A contribution to the LaTeX and Graphics contest - General command in MatlabThis is the red thread of this article. Exporting any figure is done with the following Matlab command: matlab2tikz( 'MyFig.tikz', 'height', '\fheight', 'width', '\fwidth'); SubplotsWhen creating several plots in one figure, underneath or next to each other, the conversion with Assuming you have a figure with two subplots underneath each other, the exported TikZ file will have to height=\fheight The solution is to find the second height=0.5\fheight Before and after:
Axis ticks and ticklabelsThe following problem comes more from the TikZ/pgfplots side. When the plotting range (e.g. the minimal and maximal value along the vertical axis) is too large, TikZ will leave out some axis ticks. This on its turn affects the grid (if any) in your figure. To specifiy custom ticks along the vertical axis (y axis), add the following to the ytick={1,2,3,4}
This will only create axis entries (ticks) at the values 1 to 4. In order to rename the defined ticks, use the yticklabels={a,b,c,d}
Like this, the tick '1' will be replaced by 'a', '2' by 'b', etc. Before and after:
LegendsI often find it convenient to have the legend entries all in one line (i.e. a horizontal legend, rather than vertical). In order to do this, add the following to the legend columns=-1 This needs to be placed before the legend style={/tikz/every even column/.append style={column sep=0.3cm}}
Lastly, too save space I change the font size of my legend to legend style={font=\footnotesize}
Before and after:
About the Author:Frits is a MSc student Mechanical Engineering in Delft, the Netherlands. During the BSc period of his study, he started to learn LaTeX. Soon he got hooked to this way of typesetting documents. As he was one of the few BSc students that used LaTeX, people asked him to teach them how to use LaTeX. This gave him the idea of setting up a blog. The website howToTeX.com is aimed for LaTeX beginners and is filled with tips, tricks and templates. He also is co-author of the 'five minute guide to LaTeX'. Once in a while, he writes about more advanced topics like his guide to improve LaTeX productivity. |
Latest Forum Posts
Spacing between LOF captions
23/05/2013 01:12, gilmour
Re: Equivalent of bounding box when compiling with pdflatex
22/05/2013 23:52, cgnieder
Re: how to don't reset the cite counter between chapters?
22/05/2013 23:38, Xire
Equivalent of bounding box when compiling with pdflatex
22/05/2013 23:18, samshort25
Re: Line Breaks in Local Layout Text Styles (XeTeX & CJK)
22/05/2013 19:47, dreamon
Re: Bibliography Items numbered in Order of Citations
22/05/2013 18:33, localghost
Bibliography Items numbered in Order of Citations
22/05/2013 18:22, mayankmi
Re: Section Numbers and Names in Header
22/05/2013 12:59, localghost
Re: Section Numbers and Names in Header
22/05/2013 12:51, TheTobinator
Re: Section Numbers and Names in Header
22/05/2013 12:32, localghost










Comments
I have ticks in the range of 0 and 0.02. In Matlab I have ticks like 0.005, 0.01... But in Tikz/Latex/PDF i get 2*10^-3, 4*10^-3 and this looks ugly.
A small Example:
t = [0:1e-4:0.02]; g=sin(2*pi*50*t); plot(t,g);
matlab2tikz( 'test.tex')