更改由 Matlab 生成的 LaTeX 中的分数样式

更改由 Matlab 生成的 LaTeX 中的分数样式
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}

我得到了正确答案,但其格式不同:我怎样才能获得像 A 这样的原始格式。

在此处输入图片描述

最后但并非最不重要的一点是,我如何添加表格的水平线和垂直线以及标签列和行。表格的内容每次都应通过输入公式1进行更新。

答案1

如果您希望矩阵以十进制值显示,您可以简单地重新定义\frac{}{}宏来为您生成十进制结果。

您只需调用\ConverFracToDecimal,即可获得下面的第一个结果。无需使用,即可\ConverFracToDecimal获得第二个结果:

在此处输入图片描述

笔记:

代码:

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

答案2

正在使用的乳胶输出来自符号工具箱

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

如果您使用评论中提到的替代乳胶输出功能

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

然后你得到

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

以及排版输出

在此处输入图片描述

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

答案3

我相信(大部分)你所要求的是为了增加分数之间的间距。

这个问题,你想要做的是增加\arraystrech

这是可以做到的非常很简单,只需 \renewcommand*{\arraystretch}{1.5}在开始处插入即可:

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

给予

输出

要在分数之间创建线条,请在任意\hline数量的后面使用。这样可以得到\\

再次输出

添加|(如在tabular环境中,例如\begin{array}{c|c|c|c|c})会产生垂直线:

更多产出


计算分数要复杂得多,但至少可以用pgf/来完成tikz。(我以为calc会起作用,但我从未用过它。)

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

生产

莫尔

不幸的是,这不适用于较大的数字,但这个概念是合理的,而且可能有用。如果你想在生活中保持理智,我会选择 Peter 的答案来进行计算。

相关内容