3
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.

enter image description here

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.

Sean Allred
  • 27,421
user31177
  • 1,089
  • 1
    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) – David Carlisle Dec 20 '12 at 22:16
  • @ David Carlisle done. – user31177 Dec 20 '12 at 22:22
  • If you don't use sym() in Matlab, is the result better? – Torbjørn T. 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))) – David Carlisle Dec 20 '12 at 22:25
  • ??? Undefined function or method 'latex' for input arguments of type 'double'. – user31177 Dec 20 '12 at 22:28
  • Is the latex function not in the current folder in Matlab or its path? David's suggestion works fine here. – Torbjørn T. Dec 20 '12 at 22:31
  • ??? Undefined function or method 'latex' for input arguments of type 'double'.

    Error in ==> table at 12 latex_table =latex(double(sym(A)))

    – user31177 Dec 20 '12 at 22:36
  • Typical error if latex.m is not in either the current folder of Matlab, or the path. File --> Set Path, add the folder where you placed latex.m. But you've used it before, haven't you? Surely you know what you did to make it work then .. – Torbjørn T. Dec 20 '12 at 22:42
  • what does help latex say in matlab (I don't have that command either) – David Carlisle Dec 20 '12 at 22:42
  • @DavidCarlisle I assumed it was this one http://www.mathworks.com/matlabcentral/fileexchange/2832-latex (Oh, and you're right about sym being from the symbolic math toolbox by the way.) – Torbjørn T. Dec 20 '12 at 22:44
  • A=rand(5);

    latex_table = latex(sym(A)) % it works latex_table =latex(double(sym(A))) % error

    – user31177 Dec 20 '12 at 22:45
  • @TorbjørnT. so applying sym and coercing to double is probably same as not doing anything? – David Carlisle Dec 20 '12 at 22:45
  • @DavidCarlisle Yep, latex(double(sym(A))) gives the same as latex(A). – Torbjørn T. Dec 20 '12 at 22:46
  • @user31177 And if you drop both sym and double? I.e. latex(A)? – Torbjørn T. Dec 20 '12 at 22:47
  • the exanples on the latex function you showed include latex(toeplitz(1:4),'%i','%.2f') so you can apply a decimal format string so format to as many decimal places as you want. @user31177 is that the latex function you are using or is it a differnt one? – David Carlisle Dec 20 '12 at 22:48
  • @ i 'm using built in latex function. – user31177 Dec 20 '12 at 23:02
  • @user31177 Sorry, my mistake. I've never used that toolbox much, and I wasn't aware that it contained that function. – Torbjørn T. Dec 20 '12 at 23:04

3 Answers3

4

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

enter image description here

\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}
David Carlisle
  • 757,742
4

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:

enter image description here

Notes:

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}

Peter Grill
  • 223,288
2

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}
    \newcommand{\eqA}{{\renewcommand*{\arraystretch}{1.5}
            \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

output

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

output again

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

even more output


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

moar

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.

Sean Allred
  • 27,421
  • Unfortunately, OP wants to get the decimal point numbers instead of fractions. – percusse 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. – egreg 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? – Sean Allred Dec 20 '12 at 23:05
  • Don't bother. It's matlab after all it will try to break down all the beauty. :) – percusse 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. – Sean Allred Dec 20 '12 at 23:10
  • And witness the horror. I'd also like to see if it can be done. :P – Sean Allred Dec 20 '12 at 23:14
  • Oh it can be done but you have to first strip off the numbers and that requires locally in a group overriding frac macro to give you the number themselves then since they are integers you can perform the division with calc, pgfmath etc. ... Oops Peter already did it. – percusse Dec 20 '12 at 23:25
  • I hate everything. XD Well at least I learned something :D – Sean Allred Dec 20 '12 at 23:34
  • If you turn on the fpu library it can handle big numbers too. But it needs more care :) – percusse Dec 20 '12 at 23:35