MaTeX:在Mathematica的图片中插入LaTeX代码的工具

学习学习 专栏收录该内容
669 篇文章 0 订阅

Mathematica图片中对laTeX的支持实在不敢恭维。连Geogebra都比Mathematica强。


今天搜了一下,发现有这个工具可以用,看上去不错。

作者说32位的Windows下可能无法使用,所以,如果是win32就暂时不用尝试了;不过,我尝试了下,还是搞定了大笑

配置的时候,需要用这样的代码,而不是作者所提到的,不论32位还是64位windows都必须这样,很奇怪。仅供参考。


我把涉及到的GhostScript 920的windows版安装程序(32bit ,64bit)以及MaTeX源文件1.1.0版放在CSDN供下载。

ConfigureMaTeX[ "pdfLaTeX" ->  "X:\\Program Files\\CTEX\\MiKTeX\\miktex\\bin\\pdflatex.exe", 
 "Ghostscript" -> "X:\\Program Files\\gs\\gs9.18\\bin\\gswin32c.exe"]


下载的github地址:https://github.com/szhorvat/MaTeX/releases

https://github.com/szhorvat/MaTeX/releases


https://github.com/szhorvat/MaTeX/archive/v1.6.3.zip

当前1.60

https://github.com/szhorvat/MaTeX/releases/download/v1.6.0/MaTeX-1.6.0.zip


建议使用1.3.0以前的版本;之后的版本作者把它们改得使用起来颇为繁琐。本来只是为了实现一个小小的功能,偏要添加太多复杂的花头。简单实用才好。

当前1.3.0版本

当前1.2.0版本

当前1.1.0版本

主页和介绍

http://szhorvat.net/pelican/latex-typesetting-in-mathematica.html

LaTeX typesetting in Mathematica

Thu 05 March 2015
By szhorvat

In Notes.

tags: mathematica

Mathematica is an excellent and flexible visualization tool, and even supports displaying complex mathematical formulae. However, its typesetting quality is not on par with LaTeX. The visual style is not a good match for inclusion in LaTeX documents either. To improve the quality of my figures, I wrote a small Mathematica package that makes it easy to use LaTeX-generated labels: MaTeX.

Update: The post below has been updated for MaTeX version 1.1.0. The changelog is available on GitHub.

There are already several solutions for this problem, such as PSTricks or drawing axes and labels withPGFPlots. None of these solutions make it easy to use a Mathematica-centric workflow though. MaTeX makes generating LaTeX-typeset expressions as simple as

I originally wrote these functions for my own needs, but seeing that others may be interested in them too, I wrapped them up into a package. Feel free to leave comments below and keep in mind that this it is still a work in progress.

Installation

To install MaTeX, download the latest release and drop the MaTeX.m file in the directory opened bySystemOpen@FileNameJoin[{$UserBaseDirectory, "Applications"}].

You will also need:

  • A TeX system that includes pdflatex with the standalone and lmodern packages. Both TeX Live and MiKTeX should work.
  • Ghostscript 9.15 or later.(Ghostscript 9.20)

    On OS X, MacTeX 2015 includes a compatible version of Ghostscript. If you use an older TeX distribution on OS X, get a recent Ghostscript from Richard Koch’s page.

When loading MaTeX for the first time, it will try to automatically detect TeX and Ghostscript. If this fails, it will display instructions on configuring the location of the pdflatex and gs executables using theConfigureMaTeX command. As an example, on my OS X system I needed to use the following configuration:

ConfigureMaTeX["pdfLaTeX" -> "/Library/TeX/texbin/pdflatex", "Ghostscript" -> "/usr/local/bin/gs"]

The existing configuration can always be queried using the command ConfigureMaTeX[].

Note: On Windows use the c-suffixed command line Ghostscript executable, i.e. gswin64c.exe instead ofgswin64.exe.

If you prefer, you can use xelatex instead of pdflatex. Unlike plain pdfLaTeX, XeLaTeX can load any installed system font.

MaTeX should now be ready to use. Test it using MaTeX["x^2"].

Usage examples

First we need to load MaTeX:

<<MaTeX`

My primary use for it is creating figure annotations, as below:

texStyle = {FontFamily -> "Latin Modern Roman", FontSize -> 12};
ContourPlot[x^2 + y^4 == 1, {x, -1.2, 1.2}, {y, -1.2, 1.2},
 BaseStyle -> texStyle,
 Epilog -> {
     Arrow[{{0.1, 0.3}, {0.5, 0.80}}],
     Inset[MaTeX["x^2+y^4=1", Magnification -> 2], {0.1, 0.3}, Scaled[{0.5, 1}]]
    }]

Here I used the Latin Modern font for tick labels for visual consistency with the MaTeX/LaTeX output.

We can also use MaTeX to generate beautifully typeset frame labels and frame ticks. Mathematica’s default frame and axes style is dark grey, while MateX outputs black. The BlackFrame style below makes the frames black too for consistency.

Plot[Sin[x], {x, 0, 2 Pi},
 Frame -> True, FrameStyle -> BlackFrame,
 FrameTicks -> {{Automatic, None},
                {Table[{x, MaTeX[x, "DisplayStyle" -> False]}, {x, Pi/4 Range[0, 8]}], None}},
 FrameLabel -> MaTeX /@ {"x", "\\sin x"},
 BaseStyle -> texStyle]

The MaTeX function can be used either with a string containing math-mode LaTeX code, or with an arbitrary Mathematica expression. It will automatically apply TeXForm to non-string expressions.

When writing TeX code in Mathematica strings, remember to always escape backslashes. Thus \sum must be written as "\\sum".

Advanced usage

Options

  • "DisplayStyle".  By default display style is used. Use "DisplayStyle" -> False for inline style.

    Inline formatting looks like  k=11k2=π26 ∑k=1∞1k2=π26. Display style looks like  k=11k2=π26 ∑k=1∞1k2=π26.

  • FontSize.  Use it to set the font size. Note that LaTeX uses different glyph shapes for different font sizes to improve readability. Depending on the font used, only standard sizes may be available with this option. Use Magnification instead for proportional scaling.

  • "Preamble".  This is a list of lines to be included in the LaTeX preamble. The default is{"\\usepackage{lmodern,exscale}", "\\usepackage{amsmath,amssymb}"}. The AMS packages are included by default because they may be required for compiling the output of Mathematica’s TeXForm, while lmodernprovides vector fonts and flexible font sizing. On most systems "\\usepackage{lmodern,exscale}" can be omitted.

    This option is most convenient to set permanently for the session, for example:

    SetOptions[MaTeX, "Preamble" -> {"\\usepackage{amsmath,amssymb}", "\\usepackage{color,txfonts}"}]
    MaTeX["\\color{red}\\sqrt{x}"]
  • Magnification.  Set a scaling factor for MaTeX’s output. Magnification scales the result proportionally, unlike FontSize, which will use different glyph shapes for small text.

Performance

MaTeX needs to call pdflatex for every  LATEX LATEX expression it generates. Each call might take as long as a second, which can be annoyingly slow when using MaTeX on many small expression, e.g. creating tick labels. As a partial remedy for this, MaTeX caches each result, so a second call with the same TeX code should be instantaneous.

The maximum number of expressions to be cached can be controlled using ConfigureMaTeX["CacheSize" -> ...].ClearMaTeXCache[] will clear the cache.

How does it work?

MaTeX uses pdflatex and the standalone document class to create PDF files from TeX code. The height of thePDF files is ensured to be at least the line height by including a \strut. Mathematica can import PDF files, but it can only interpret simple ones correctly. To avoid problems, all font glyphs are converted to outline curves using Ghostscript before import. This capability is new in Ghostscript 9.15, hence the version requirement.

Troubleshooting

MaTeX no longer works on OS X 10.11 El Capitan

If you use MacTeX on OS X El Capitan, the path to pdflatex or xelatex must be set as

ConfigureMaTeX["pdfLaTeX" -> "/Library/TeX/texbin/pdflatex"]

If you used the path /usr/texbin/pdflatex on OS X 10.10 or earlier, it will no longer work on 10.11. You must reconfigure MaTeX with the new path. Please see the explanation here.

The path to Ghostscript is unchanged, /usr/local/bin/gs.

Invalid syntax in or before <!DOCTYPE html>” error

This error indicates that MaTeX was not downloaded correctly. Please download the latest release of MaTeX.mfrom this page, and re-install it according to the instructions above.

MaTeX::texerr on 32-bit Windows

If you are using a 32-bit version of Mathematica on Windows, MaTeX may not work correctly and may show the following error every time the MaTeX function is used:

MaTeX::texerr: Error while running LaTeX

This is due to a bug in Mathematica for 32-bit Windows. Due to the rarity of this platform, I decided not to work around this problem. Please consider using the 64-bit version. If you must use the 32-bit version for some reason, contact me for partial workarounds.

MaTeX still doesn’t work!

Please restart Mathematica, then enter the following lines in a new notebook and evaluate them:

Needs["MaTeX`"]
ConfigureMaTeX[]
MaTeX`Developer`$Version

Save the notebook and email it to me along with a complete description of the problems you are seeing.

Comments !



  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

v1.6.3 @szhorvat szhorvat released this 25 days ago · 8 commits to master since this release More robust error checking and reporting Documentation improvements Downloads 1.02 MB MaTeX-1.6.3.paclet Source code (zip) Source code (tar.gz) v1.6.2 1ff11e8 v1.6.2 @szhorvat szhorvat released this on 20 Nov · 22 commits to master since this release The documentation is now integrated into the Documentation Center. Bug fix: full compatibility with Mathematica 10.0 restored. (Note: Re-released on 2016-11-20 with documentation fixes but no modifications to the package.) Downloads 504 KB MaTeX-1.6.2.paclet Source code (zip) Source code (tar.gz) v1.6.1 30633ef v1.6.1 @szhorvat szhorvat released this on 30 Aug · 29 commits to master since this release Bug fix: better error checking for the CacheSize option. Downloads 10.5 KB MaTeX-1.6.1.zip Source code (zip) Source code (tar.gz) v1.6.0 f2852e2 v1.6.0 @szhorvat szhorvat released this on 20 Aug · 32 commits to master since this release MaTeX now threads over lists. A list is batch-processed using a single run of LaTeX, which is much faster than element-wise processing. Implemented by Andreas Ahlrichs. Note that this changes behaviour slightly. Previous versions of MaTeX compiled MaTeX[{1, x^2, x/2}] as a single expression. Now each element of the list is converted to a separate result. To restore the old behaviour, apply TeXForm explicitly: MaTeX[TeXForm[{1, x^2, x/2}]. Expressions with head TeXForm are now automatically handled. Bug fixes: Better handling of CR/LF line endings and character encodings. Downloads 10.5 KB MaTeX-1.6.0.zip Source code (zip) Source code (tar.gz) v1.5.0 a2b4aad v1.5.0 @szhorvat szhorvat released this on 19 Jun · 49 commits to master since this release Much improved LaTeX error reporting. Please report any problems you notice with the new error reporting. MaTeX now checks for common user errors and issues warnings. Turn them off using Off[MaTeX::warn]. Downloads 9.31 KB MaTeX-1.5.0.zip Source code (zip) Source code (tar.gz) v1.4.0 97085e1 v1.4.0 @szhorvat szhorvat released this on 18 Jun · 52 commits to master since this release Separated "Preamble" and "BasePreamble" options. The default preamble is now in "BasePreamble". The "Preamble" option can be set without needing to worry about the default. Package symbols are protected MaTeX now follows the standard Mathematica package structure. This means that it now consists of multiple files. Move the entire MaTeX directory (and not just MaTeX.m) into $UserBaseDirectory/Applications to install. Downloads 8.61 KB MaTeX-1.4.0.zip Source code (zip) Source code (tar.gz) v1.3.0 3cfd39f v1.3.0 @szhorvat szhorvat released this on 16 Apr · 60 commits to master since this release Added the "TeXFileFunction" and "LogFileFunction" options for easier debugging. Set them to Print to see the generated LaTeX code or the LaTeX log file. Some code cleanup. Downloads 14.3 KB MaTeX.m Source code (zip) Source code (tar.gz) v1.2.0 68cf8c6 v1.2.0 @szhorvat szhorvat released this on 24 Feb · 64 commits to master since this release Added ContentPadding option: ContentPadding -> True ensures that the the output height is at least one line height Added LineSpacing option The size of vertical borders is slightly different now: use LineSpacing -> {0, 14.4} to reproduce the older behaviour Bug fixes in line height and baseline calculation Downloads 14 KB MaTeX.m Source code (zip) Source code (tar.gz) v1.1.1 4117fed v1.1.1 @szhorvat szhorvat released this on 22 Feb · 67 commits to master since this release Reliability fixes for Windows Windows: Work around Mathematica bug causing MaTeX to fail when the current directory has special characters in its name Windows: Ensure that autodetected paths do not use / as path separator Downloads 13.4 KB MaTeX.m Source code (zip) Source code (tar.gz) v1.1.0 7e3937a v1.1.0 @szhorvat szhorvat released this on 19 Nov 2015 · 69 commits to master since this release Syntax highlighting for MaTeX functions Attempt to auto-detect Ghostscript and pdfLaTeX on first run on Windows (in addition to Mac and Linux) Attempt to recover from a broken configuration Minor bug fixes and compatibility fixes Downloads 12.4 KB MaTeX.m Source code (zip) Source code (tar.gz)
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值