How can I convert a formatted text cell to latex code?



I have a formatted text cell, I want to export it as latex. How can I do that? Since copy as LaTeX doesn't work, I thought of pasting it into a string and exporting, but it's a no go:

 ExportString["Let M be a TM and let \!\(\*FormBox[\(A \[SubsetEqual] \
\*SuperscriptBox[\({0, 1}\), \(*\)]\),
TraditionalForm]\). We say that \!\(\*FormBox[\(M decides A\),
TraditionalForm]\) if \!\(\*FormBox[\(\[ForAll] \\\ x \[Element] \
\*SuperscriptBox[\({0, 1}\), \(*\)]\),
TraditionalForm]\), \!\(\*FormBox[\(x \[Element] A\\\  \[Implies] \\\ \
M(x) = 1\),
TraditionalForm]\), \!\(\*FormBox[\(x \[NotElement] A\\\  \[Implies] \
M(x) = 0. \),
TraditionalForm]\) We say that  \!\(\*FormBox[\(A \[SubsetEqual] \
\*SuperscriptBox[\({0, 1}\), \(*\)]\),
TraditionalForm]\) is \!\(\*
StyleBox[\"decidable\",\nFontWeight->\"Bold\"]\) if there is a TM \
that accepts it.", "TeX"]


Posted 2012-11-20T15:05:29.470

Reputation: 30 727

3Just to get the correct string before programming anything: I guess you're missing \( before the second M and \) after the following A. – einbandi – 2012-11-20T15:21:08.020

I pasted it back in. It returns $Aborted. – M.R. – 2012-11-20T20:59:57.407

The first thing I tried was copying the text cell as LaTeX, but it didn't work. Gave this message: TeXForm::unspt: TeXForm of Map[SystemFEDumpTransformBoxesToTraditionalFormBoxes[SystemFEDumpprocessBoxesForCopyAsTeX[{TextForm},TextData[#1]]]&,<<14>>, if there is a TM that accepts it.] is not supported. >> – M.R. – 2012-11-20T21:04:20.423



When you export the whole cell and not only its content, then you should get LaTeX code which is not one big math-environment. Maybe one easy way is to create a new notebook with

nb = CreateDocument[]

then you copy your text-cell in it and if you then Export is as "LaTeX", you get the correct code. For example, if I have this

enter image description here

I can simply use Export (or ExportString to show it)

ExportString[nb, "LaTeX"]

and I get

%% AMS-LaTeX Created by Wolfram Mathematica 8.0 :

\usepackage{amsmath, amssymb, graphics, setspace}



We can use Euler{'}s formula, \(e^{inx}=\cos (nx)+i\sin (nx)\) 
where \(i\) is the imaginary unit.


which gives a nice pdf:

enter image description here

Making it usable with a palette

Here is a basic approach how to pack this into a palette.

  Button["To LaTeX", copyToClipboard[
       "LaTeX"], __ ~~ "begin{document}" ~~ text__ ~~ 
        "\\end{document}" ~~ ___ :> text]]],
  Initialization :> (copyToClipboard[text_] := 
             nb = NotebookCreate[Visible -> False];
             NotebookWrite[nb, Cell[text, "Text"]];
             SelectionMove[nb, All, Notebook];
             FrontEndTokenExecute[nb, "Copy"];

Now you get a palette with a button and what you have to do is to select the text-cell with the LaTeX code and press the button. Then you have the correct form in the clipboard and can insert it.

For my above example I get

We can use Euler{'}s formula, \(e^{inx}=\cos (nx)+i\sin (nx)\) where \(i\)
is the imaginary unit.

Acknowledgements: I stole the copyToClipboard from @szabolcs image uploader!


Posted 2012-11-20T15:05:29.470

Reputation: 109 574

@M.R. I would love to get some feedback from you. Is it working? – halirutan – 2012-11-26T17:31:04.887

It is! Thank you. I'd like this to be a key command such as Item[KeyEvent["y", Modifiers -> {Control}], FrontEndExecute[FrontEnd`CellPrint[ nb = NotebookCreate[Visible -> False]; NotebookWrite[nb, Cell[StringTrim@ StringReplace[ ExportString[NotebookRead[SelectedNotebook[]], "LaTeX"], __ ~~ "begin{document}" ~~ text__ ~~ "\end{document}" ~~ ___ :> text], "Text"]]; SelectionMove[nb, All, Notebook]; FrontEndTokenExecute[nb, "Copy"]; NotebookClose[nb]; NotebookCreate[Visible -> False]; Cell["Copied!"]]] ] but I can't get it to work! – M.R. – 2012-11-26T18:19:02.223

Do you know how to add arbitrary commands into a item in – M.R. – 2012-11-26T18:19:42.190

Just search for KeyEvent on this site. We had various question regarding custom commands. Look for instance here.

– halirutan – 2012-11-26T18:33:17.650

I know about them, but the examples I have seen only use FETokens and never execute arbitrary code. – M.R. – 2012-11-26T18:38:21.187

Then this is worth a separate answer. I never tweaked my KeyEvents so this has to be answered by someone else. Sorry. – halirutan – 2012-11-26T18:48:18.243

Ok, no problem, thanks for the answer. – M.R. – 2012-11-26T18:49:57.750


Here is my primary thought. Wish it could bring some inspirations.

First suppose we already have a Text-style Cell, then use the menu Copy As -> LaTeX:


Paste the content into a pair of quotation marks and clear extra line-breaks:

str = "\\text{Let }


 \\text{, but need some text after it.}" // 
 StringReplace[#, "\n" -> ""] &

We get strings suitable for math-mode:

"\\text{Let }M\\text{ be a }\\text{TM}\\text{ and let }A\\subseteq \
\\{0,1\\}^*\\text{. We say that }M\\text{ decides }A\\text{ if \
}\\forall x\\in \\{0,1\\}^*\\text{, }x\\in A\\Rightarrow \
M(x)=1\\text{, }x\\notin A\\Rightarrow M(x)=0\\text{. We say that \
}A\\subseteq \\{0,1\\}^*\\text{ is decidable if there is a \
}\\text{TM}\\text{ that accepts it. And here is an inline complicated \
formula: }\\sum _{k=1}^n \\frac{1}{f(k)}\\int_{\\alpha }^{\\beta } \
\\sqrt{g(x)} \\, dx\\text{, but need some text after it.}"

Then we reverse the "environment":

str2 = StringReplace[str, RegularExpression["\\\\text{(.*?)}"] :> "«$1»"]

str3 = StringReplace[str2, 
   RegularExpression["»(.*?)«"] :> 
    If[StringLength["$1"] == 0, "", "\$$1\$"]] // 
  StringReplace[#, {"«" -> "", "»" -> ""}] &

Then Copy As -> Plain Text:


Past into SE editor:

Let $M$ be a TM and let $A\subseteq \{0,1\}^*$. We say that $M$ decides $A$ if $\forall x\in \{0,1\}^*$, $x\in A\Rightarrow M(x)=1$, $x\notin A\Rightarrow M(x)=0$. We say that $A\subseteq \{0,1\}^*$ is decidable if there is a TM that accepts it. And here is an inline complicated formula: $\sum _{k=1}^n \frac{1}{f(k)}\int_{\alpha }^{\beta } \sqrt{g(x)} \, dx$, but need some text after it.

Though looks working, this procedure is very primary and buggy. e.g. The $TM$s in the original Text Cell are actually in math-mode (i.e. inputted after a Ctrl+9), but they were converted to text-mode when copy as LaTeX. And when the paragraph ends with a formula, it should be take care of with special rule.


Posted 2012-11-20T15:05:29.470

Reputation: 25 336

Thanks @silvia for your solution, but I feel that the other one is simpler... – M.R. – 2012-11-26T18:50:51.090

@M.R. of course~ I absolutely agree with you :) – Silvia – 2012-11-27T15:03:53.350