# pdflatex amd Matlab LaTeX interpreter alternative to psfrag?

I use TexWorks that comes together with Tex Live 2012. After having read a lot on how to use "something similar" to psfrag with pdflatex, I have almost found my way. I have just discovered the instruction set(legend_name,'Interpreter','latex') to set a LaTeX interpreter to read what we write in the legend directly. I found it very cool!

Next, I want to include a reference in the legend (or a citation). I use the standard command \cite{bib_key} when I write the legend in Matlab, but what I get as output is the typical "[?]" since LaTeX cannot find the bib-entry. I have tried to compile with BibTex, but nothing happens. Any hints?

Using a command like \cite directly in MATLAB is a major pain. For example, if you just wanted to recreate the following MWE

\documentclass{article}
\begin{document}
\bibliographystyle{plain}
A citation: \cite{article-minimal}
\newsavebox\mytempbib
\savebox\mytempbib{\parbox{\textwidth}{\bibliography{xampl}}}
\end{document}


As I explained in this answer of mine, if in MATLAB you define

legend_name = '\bibliographystyle{plain}A citation: \cite{article-minimal}\newsavebox\mytempbib\savebox\mytempbib{\parbox{\textwidth}{\bibliography{xampl}}}';


then the MATLAB LaTeX interpreter would convert it into

\nofiles
\documentclass{mwarticle}
\begin{document}
\setbox0=\hbox{%
\bibliographystyle{plain}%
A citation: \cite{article-minimal}%
\newsavebox\mytempbib%
\savebox\mytempbib{\parbox{\textwidth}{\bibliography{xampl}}}%
}\copy0\special{bounds: \the\wd0 \the\ht0 \the\dp0}
\end{document}


where the mwarticle class is essentially the article class without sizing commands and margins. The \nofiles, along with not being able to find xampl.bib, causes this to fail. Even if you rewrite tex.m to not insert the \nofiles, you still have the issue of needing BibTex and multiple passes. The underlying MATLAB texmex function is closed source so modifying it to handle BibTex and multiple passes would be difficult and probably violate the MATLAB EULA.

The easiest option would probably be to overload tex.m to read a dvi file. You can either reprogram tex.m to call your locally installed LaTeX engine, or simply make it use an already existing dvi file.

function [dviout,errout,auxout] = tex(varargin)
fid = fopen('test.dvi');
dviout = uint8(dviout);
fclose(fid);
errout = [];
auxout = [];
end


If you then create test.dvi

\documentclass{article}
\setlength\topmargin{-0.5in}
\setlength\oddsidemargin{0in}
\begin{document}
\setbox0=\hbox{%
\bibliographystyle{plain}
A citation: \cite{article-minimal}
\newsavebox\mytempbib
\savebox\mytempbib{\parbox{\textwidth}{\bibliography{xampl}}}
}\copy0\special{bounds: \the\wd0 \the\ht0 \the\dp0}
\end{document}


You then get the result you want in MATLAB

plot(1:10);
legend({'DOES NOT MATTER'}, 'Interpreter', 'LaTeX')


A more robust, but more complicated solution, would be to fully hack tex.m to call latex and bibtex for you and handle the multiple passes.

• I see. I thought it was something simpler to do, but I don't feel to hack any of the existing Matlab files (i.e. tex.m). I rather keep the originals. Well, at this point I will write the number of citation by hand, like legend('Method in [11]', 'Interpreter', 'LaTeX') without using any \cite command when defining the legend in Matlab. I have also tried to use .eps files together with XeLaTex, but it didn't work out. – Ubaldo Tiberi Oct 15 '13 at 10:35