# Changing fraction style in LaTeX as generated by Matlab

A= [0.7577    0.7060    0.8235    0.4387    0.4898
0.7431    0.0318    0.6948    0.3816    0.4456
0.3922    0.2769    0.3171    0.7655    0.6463
0.6555    0.0462    0.9502    0.7952    0.7094
0.1712    0.0971    0.0344    0.1869    0.7547]
latex_table = latex(sym(A));
I save it as eqA.
equations1.tex
\newcommand{\eqA}{
\left(
\begin{array}{ccccc}
\frac{6825116339432507}{9007199254740992} &
\frac{6359497797823013}{9007199254740992} &
\frac{3708524368810113}{4503599627370496} &
\frac{987964467329731}{2251799813685248} &
\frac{4411405500742427}{9007199254740992}\\
\frac{6693542213068579}{9007199254740992} &
\frac{71681197541747}{2251799813685248} &
\frac{6258459855040489}{9007199254740992} &
\frac{859193262592067}{2251799813685248} &
\frac{1003370923741521}{2251799813685248}\\
\frac{55201045594335}{140737488355328} &
\frac{2494300503760379}{9007199254740992} &
\frac{2856178200482939}{9007199254740992} &
\frac{1723790560926853}{2251799813685248} &
\frac{2910735031501795}{4503599627370496}\\
\frac{2952009981953243}{4503599627370496} &
\frac{415874915283285}{9007199254740992} &
\frac{8558839330135289}{9007199254740992} &
\frac{3581261978445933}{4503599627370496} &
\frac{6389390375844301}{9007199254740992}\\
\frac{770956303438939}{4503599627370496} &
\frac{874885307559191}{9007199254740992} &
\frac{77565677658637}{2251799813685248} &
\frac{1683198784473707}{9007199254740992} &
\frac{6797613319514473}{9007199254740992}
\end{array}
\right)
}

//latex
\documentclass{article}
\input{equations1}

\begin{document}
Here we have a nice matrix:
$\eqA{}$

\end{document}


I ' m getting right answer but its different format: How can I get original format like A.

Last but not least, how can I add horizontal and vertical lines and lable colume and rows of the table. The contents of the table should be updated every time by input equation1.

• Please edit your question to make a latex document that we can use locally to reproduce the matrix shown. (eg add the contents of equation1.tex to your question) Dec 20 '12 at 22:16
• @ David Carlisle done. Dec 20 '12 at 22:22
• If you don't use sym() in Matlab, is the result better? Dec 20 '12 at 22:23
• I don't have a sym command in matlab (is that the symbolic toolbox) It is presumably returning a matrix of fractions, if you want a matrix of double as in your original matrx it would be easer to coerce the matrix to double in matlab before converting to latex so (untested) latex(double(sym(A))) Dec 20 '12 at 22:25
• ??? Undefined function or method 'latex' for input arguments of type 'double'. Dec 20 '12 at 22:28

If you want the matrix to be displayed with decimal values, you could simply redefine the \frac{}{} macro to produce the decimal result for you.

All you need to do is to call \ConverFracToDecimal and you will get the first result below. Without using \ConverFracToDecimal you obtain the second result:

## Code:

\documentclass{article}
\usepackage{fp}
\usepackage{siunitx}

\newcommand{\ConverFracToDecimal}{%
\renewcommand*{\frac}[2]{%
\FPdiv\Result{##1}{##2}%
\num[round-mode=places,round-precision=4]{\Result}%
}%
}%

\newcommand{\eqA}{
\left(
\begin{array}{ccccc}
\frac{6825116339432507}{9007199254740992} &
\frac{6359497797823013}{9007199254740992} &
\frac{3708524368810113}{4503599627370496} &
\frac{987964467329731}{2251799813685248} &
\frac{4411405500742427}{9007199254740992}\\
\frac{6693542213068579}{9007199254740992} &
\frac{71681197541747}{2251799813685248} &
\frac{6258459855040489}{9007199254740992} &
\frac{859193262592067}{2251799813685248} &
\frac{1003370923741521}{2251799813685248}\\
\frac{55201045594335}{140737488355328} &
\frac{2494300503760379}{9007199254740992} &
\frac{2856178200482939}{9007199254740992} &
\frac{1723790560926853}{2251799813685248} &
\frac{2910735031501795}{4503599627370496}\\
\frac{2952009981953243}{4503599627370496} &
\frac{415874915283285}{9007199254740992} &
\frac{8558839330135289}{9007199254740992} &
\frac{3581261978445933}{4503599627370496} &
\frac{6389390375844301}{9007199254740992}\\
\frac{770956303438939}{4503599627370496} &
\frac{874885307559191}{9007199254740992} &
\frac{77565677658637}{2251799813685248} &
\frac{1683198784473707}{9007199254740992} &
\frac{6797613319514473}{9007199254740992}
\end{array}
\right)
}

\begin{document}
$\ConverFracToDecimal\eqA$

\bigskip
Without caling \verb|\ConverFracToDecimal| you get:
\medskip

$\eqA$
\end{document}


The latex output being used is from the symbolic toolbox

http://www.mathworks.co.uk/help/symbolic/latex.html

If you use the alternative latex output function mentioned in comments

http://www.mathworks.com/matlabcentral/fileexchange/2832-latex

then you get

>> latex(A,'nomath')
0.75770 & 0.70600 & 0.82350 & 0.43870 & 0.48980 \\
0.74310 & 0.031800 & 0.69480 & 0.38160 & 0.44560 \\
0.39220 & 0.27690 & 0.31710 & 0.76550 & 0.64630 \\
0.65550 & 0.046200 & 0.95020 & 0.79520 & 0.70940 \\
0.17120 & 0.097100 & 0.034400 & 0.18690 & 0.75470


and a typeset output of

\documentclass{article}
\usepackage{amsmath}
\begin{document}
$\begin{pmatrix} 0.75770 & 0.70600 & 0.82350 & 0.43870 & 0.48980 \\ 0.74310 & 0.031800 & 0.69480 & 0.38160 & 0.44560 \\ 0.39220 & 0.27690 & 0.31710 & 0.76550 & 0.64630 \\ 0.65550 & 0.046200 & 0.95020 & 0.79520 & 0.70940 \\ 0.17120 & 0.097100 & 0.034400 & 0.18690 & 0.75470 \end{pmatrix}$
\end{document}


I believe (most of) what you're asking is to increase the spacing between the fractions.

From this question, what you want to do is increase the \arraystrech.

This can be done very easily, just insert \renewcommand*{\arraystretch}{1.5} right at the start:

\documentclass{article}
\usepackage{amsmath}
\left(
\begin{array}{ccccc}
\frac{6825116339432507}{9007199254740992} &
\frac{6359497797823013}{9007199254740992} &
\frac{3708524368810113}{4503599627370496} &
\frac{987964467329731}{2251799813685248} &
\frac{4411405500742427}{9007199254740992}\\
\frac{6693542213068579}{9007199254740992} &
\frac{71681197541747}{2251799813685248} &
\frac{6258459855040489}{9007199254740992} &
\frac{859193262592067}{2251799813685248} &
\frac{1003370923741521}{2251799813685248}\\
\frac{55201045594335}{140737488355328} &
\frac{2494300503760379}{9007199254740992} &
\frac{2856178200482939}{9007199254740992} &
\frac{1723790560926853}{2251799813685248} &
\frac{2910735031501795}{4503599627370496}\\
\frac{2952009981953243}{4503599627370496} &
\frac{415874915283285}{9007199254740992} &
\frac{8558839330135289}{9007199254740992} &
\frac{3581261978445933}{4503599627370496} &
\frac{6389390375844301}{9007199254740992}\\
\frac{770956303438939}{4503599627370496} &
\frac{874885307559191}{9007199254740992} &
\frac{77565677658637}{2251799813685248} &
\frac{1683198784473707}{9007199254740992} &
\frac{6797613319514473}{9007199254740992}
\end{array}
\right)}
}

\begin{document}
$\eqA$
\end{document}


giving

To create lines between the fractions, use an \hline after as many \\ as you like. This gives

Adding | (as in a tabular environment, eg \begin{array}{c|c|c|c|c}) produces the vertical lines:

Evaluating the fractions is a fair bit more complicated, but can be at least accomplished with pgf/tikz. (I would assume calc would work, but I've never worked with it.)

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}

\newcommand{\evalfrac}[2]{\pgfmathparse{#1/#2}\pgfmathresult}

\newcommand{\eqA}
{
\ensuremath
{
\renewcommand*{\arraystretch}{1.5}
\let\oldfrac\frac             % set up a temporary holder for the normal frac
\let\frac\evalfrac            % make frac the evalfrac
\left(
\begin{array}{c|c}
\frac{1}{4} &
\frac{2}{4}\\ \hline
\frac{3}{4} &               % do what you do best
\frac{4}{4}
\end{array}
\right)
\let\frac\oldfrac             % make frac happy again
}
}

\begin{document}
$\eqA$

Test. Another test fraction $\frac{1}{2}$.
\end{document}


produces

This unfortunately does not work with larger numbers, but the concept is sound and potentially useful. If you'd like to keep some sanity in your life, I'd go with Peter's answer for the calculation bits.

• Unfortunately, OP wants to get the decimal point numbers instead of fractions. Dec 20 '12 at 22:54
• pmatrix is better than \left(\begin{array}...\end{array}\right); I'd also add a pair of braces around the whole thing, in order to make the change to \arraystretch local. Dec 20 '12 at 22:55
• @percusse, whoops. Not a big matlab guy - mathematica all the way ;) The question can definitely be made more clear as to what the OP wants - I thought he wanted a LaTeX way to do it. (Still researching that btw - calc package? Dec 20 '12 at 23:05
• Don't bother. It's matlab after all it will try to break down all the beauty. :) Dec 20 '12 at 23:08
• but matlab!! ew!! At least with latex you can throw it in a common preamble and forget about it. With matlab, you have to keep opening it up. Dec 20 '12 at 23:10