23

Whereas

  • Mathematica allows us to dump an entire notebook into a readable LaTeX file. However, the formatting of that file is not optimal.
  • There are many posts on how to "beautify" a single Mathematica equation into LaTeX. But, this procedure can not be manually implemented for a notebook containing over 100 in/out.

So my question is whether there is a LaTeX style sheet which can take the .tex file dumped by Mathematica and display it in a more appealing way.

Following is a sample of the generated code:

%% AMS-LaTeX Created by Wolfram Mathematica 9.0 : www.wolfram.com

\documentclass{article}
\usepackage{amsmath, amssymb, graphics, setspace}

\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}

\begin{document}

\section*{Define the load (omitting the d$\theta $) { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } }

\begin{doublespace}
\noindent\(\pmb{\text{dP}'=w R;}\)
\end{doublespace}

\section*{Primary Structure; Externally Applied load { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } { } }

\subsection*{\textup{ Determine horizontal reaction at B by taking moment with respect to Z (CCW +ve)}}

\begin{doublespace}
\noindent\(\pmb{\text{x1}=R \text{Cos}[\alpha ]; \text{x2}=R \text{Cos}[\theta ]; \text{y0}=R(1-\text{Sin}[\alpha ]);}\)
\end{doublespace}

\begin{doublespace}
\noindent\(\pmb{\text{SigMZ}= -\text{BX} \text{y0}+\int _{\alpha }^{\pi /2}(\text{x1}-\text{x2}) \text{dP}' d\theta }\)
\end{doublespace}

\begin{doublespace}
\noindent\(\frac{1}{2} R^2 w ((\pi -2 \alpha ) \text{Cos}[\alpha ]+2 (-1+\text{Sin}[\alpha ]))-\text{BX} R (1-\text{Sin}[\alpha ])\)
\end{doublespace}

\begin{doublespace}
\noindent\(\pmb{\text{Sol1}=\text{Solve}[\text{SigMZ}\text{==}0, \text{BX}]; \text{BX}= \text{BX}\text{/.}\text{Sol1}[[1]]}\)
\end{doublespace}

\begin{doublespace}
\noindent\(-\frac{R w (-2+\pi  \text{Cos}[\alpha ]-2 \alpha  \text{Cos}[\alpha ]+2 \text{Sin}[\alpha ])}{2 (-1+\text{Sin}[\alpha ])}\)
\end{doublespace}

\subsection*{\textup{ Determine vertical reaction by taking summation of forces in the y direction}}

\begin{doublespace}
\noindent\(\pmb{\text{BY}=\int _{\alpha }^{\pi /2} \text{dP}'d\theta }\)
\end{doublespace}

\begin{doublespace}
\noindent\(R w \left(\frac{\pi }{2}-\alpha \right)\)
\end{doublespace}

\begin{doublespace}
\noindent\(\pmb{\text{CX}=\text{BX}}\)
\end{doublespace}

\begin{doublespace}
\noindent\(-\frac{R w (-2+\pi  \text{Cos}[\alpha ]-2 \alpha  \text{Cos}[\alpha ]+2 \text{Sin}[\alpha ])}{2 (-1+\text{Sin}[\alpha ])}\)
\end{doublespace}


\end{document}
12
  • 4
    This is actually a pretty interesting question; +1 :) Can you give us a short example of the kind of document Mathematica outputs? Depending on how Mathematica handles it, this could be easy. Otherwise, there's always the preprocessing option. Commented Apr 25, 2014 at 14:52
  • For mathematica docs(not latex), I... let here 2 links in spanish: youtube.com/watch?v=XTBIY8rr9WI youtube.com/watch?v=voU9tJIG6MI
    – Mika Ike
    Commented Apr 25, 2014 at 14:59
  • 1
    Well this is the "ugly" latex code generated by mathematica:
    – victor
    Commented Apr 25, 2014 at 22:11
  • 3
    @victor Who has implemented the conversion tools doesn't know even the basics of TeX nor of math notation, where the cosine of an angle is written \cos\alpha and surely not \text{Cos}[\alpha]. :-(
    – egreg
    Commented Apr 25, 2014 at 22:28
  • 3
    @egreg \text{Cos}[\alpha] is in fact reasonable. They're trying to replicate what's displayed in a Mathematica notebook, where cosine in the default output style (called StandardForm in Mathematica jargon) is Cos[α]. If you ask Mathematica to display in TraditionalForm, then the LaTeX output for Cos[x]//TraditionalForm will be \noindent\(\cos (x)\), which is more TeXish.
    – 4ae1e1
    Commented May 2, 2014 at 4:41

2 Answers 2

4

In terms of the equation positioning, I've added the fleqn option to the document class, but apart from that, the following is the only additions to the preamble to format the document contents:

% New stuff
\usepackage{environ}
\renewcommand{\pmb}[1]{#1}% Remove bold math
\RenewEnviron{doublespace}{%
  \let\noindent\relax%
  \let\(\relax\let\)\relax%
  \[\BODY\]
}

\let\oldtext\text
\renewcommand{\text}[1]{%
  \ifnum\pdfstrcmp{#1}{Sin}=0
    \cos
  \else\ifnum\pdfstrcmp{#1}{Cos}=0
    \sin
  \else\ifnum\pdfstrcmp{#1}{Tan}=0
    \tan
  \else\ifnum\pdfstrcmp{#1}{==}=0
    \equiv
  \else
    \oldtext{#1}
  \fi\fi\fi\fi
}

The intent is to

  • remove the use of \pmb in math context, since I don't know what it would be used for;

  • remove the doublespace environment's use as a display math equation. Instead, remove the use of \noindent, \( and \) and rather set the contents inside \[...\];

  • update \text to condition on its argument, thereby replacing \text{Cos} by \cos, as well as other possible constructions.

This is for sure not perfect, and could be improved. I also assumed that the superfluous spacing inside sectional titles is due to the author adding them, and should be removed manually.

Here's a complete translation of the document:

enter image description here

%% AMS-LaTeX Created by Wolfram Mathematica 9.0 : www.wolfram.com

\documentclass[fleqn]{article}
\usepackage{amsmath, amssymb, graphics, setspace}

\newcommand{\mathsym}[1]{{}}
\newcommand{\unicode}[1]{{}}

% New stuff
\usepackage{environ}
\renewcommand{\pmb}[1]{#1}% Remove bold math
\RenewEnviron{doublespace}{%
  \let\noindent\relax%
  \let\(\relax\let\)\relax%
  \[\BODY\]
}

\let\oldtext\text
\renewcommand{\text}[1]{%
  \ifnum\pdfstrcmp{#1}{Sin}=0
    \cos
  \else\ifnum\pdfstrcmp{#1}{Cos}=0
    \sin
  \else\ifnum\pdfstrcmp{#1}{Tan}=0
    \tan
  \else\ifnum\pdfstrcmp{#1}{==}=0
    \equiv
  \else
    \oldtext{#1}
  \fi\fi\fi\fi
}

\begin{document}

\section*{Define the load (omitting the d$\theta $)}

\begin{doublespace}
\noindent\(\pmb{\text{dP}'=w R;}\)
\end{doublespace}

\section*{Primary Structure; Externally Applied load}

\subsection*{\textup{Determine horizontal reaction at B by taking moment with respect to Z (CCW +ve)}}

\begin{doublespace}
\noindent\(\pmb{\text{x1}=R \text{Cos}[\alpha ]; \text{x2}=R \text{Cos}[\theta ]; \text{y0}=R(1-\text{Sin}[\alpha ]);}\)
\end{doublespace}

\begin{doublespace}
\noindent\(\pmb{\text{SigMZ}= -\text{BX} \text{y0}+\int _{\alpha }^{\pi /2}(\text{x1}-\text{x2}) \text{dP}' d\theta }\)
\end{doublespace}

\begin{doublespace}
\noindent\(\frac{1}{2} R^2 w ((\pi -2 \alpha ) \text{Cos}[\alpha ]+2 (-1+\text{Sin}[\alpha ]))-\text{BX} R (1-\text{Sin}[\alpha ])\)
\end{doublespace}

\begin{doublespace}
\noindent\(\pmb{\text{Sol1}=\text{Solve}[\text{SigMZ}\text{==}0, \text{BX}]; \text{BX}= \text{BX}\text{/.}\text{Sol1}[[1]]}\)
\end{doublespace}

\begin{doublespace}
\noindent\(-\frac{R w (-2+\pi  \text{Cos}[\alpha ]-2 \alpha  \text{Cos}[\alpha ]+2 \text{Sin}[\alpha ])}{2 (-1+\text{Sin}[\alpha ])}\)
\end{doublespace}

\subsection*{\textup{Determine vertical reaction by taking summation of forces in the y direction}}

\begin{doublespace}
\noindent\(\pmb{\text{BY}=\int _{\alpha }^{\pi /2} \text{dP}'d\theta }\)
\end{doublespace}

\begin{doublespace}
\noindent\(R w \left(\frac{\pi }{2}-\alpha \right)\)
\end{doublespace}

\begin{doublespace}
\noindent\(\pmb{\text{CX}=\text{BX}}\)
\end{doublespace}

\begin{doublespace}
\noindent\(-\frac{R w (-2+\pi  \text{Cos}[\alpha ]-2 \alpha  \text{Cos}[\alpha ]+2 \text{Sin}[\alpha ])}{2 (-1+\text{Sin}[\alpha ])}\)
\end{doublespace}

\end{document}
3
  • Thanks, much much better. However how to remove all that blank space in between the equations (i.e. make them single space for instance).
    – victor
    Commented Jul 22, 2014 at 21:52
  • @victor: You could probably add \setlength{\parindent}{0pt} to your preamble and change the doublespace environment to use $\displaystyle\BODY$ \\ instead of \[\BODY\].
    – Werner
    Commented Jul 23, 2014 at 16:27
  • At the risk of sounding too "greedy", lsat but not least how to discernate the input from the output (with the number if possible). Once you have all those questions resolved, might as well put together a nice package to transfer mathematica code into latex (very badly needed).
    – victor
    Commented Jul 24, 2014 at 20:35
2

You could export your whole Mathematica notebook to pdf and then with pdfpage pack use it in the latex doc

preamble:

\usepackage[final]{pdfpages}
.
.
.
\begin{document}

\includepdf[pages=1]{notebook.pdf}
2
  • Thanks, I already do that, but it occupies too much space.
    – victor
    Commented May 1, 2014 at 5:04
  • This was my solution when I faced this problem. Yes, it makes for larger file size which is the downside. Commented Jun 25, 2014 at 3:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .