8
$\begingroup$

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"]
$\endgroup$
3
  • 3
    $\begingroup$ Just to get the correct string before programming anything: I guess you're missing \( before the second M and \) after the following A. $\endgroup$
    – einbandi
    Commented Nov 20, 2012 at 15:21
  • $\begingroup$ I pasted it back in. It returns $Aborted. $\endgroup$
    – M.R.
    Commented Nov 20, 2012 at 20:59
  • $\begingroup$ 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. >> $\endgroup$
    – M.R.
    Commented Nov 20, 2012 at 21:04

2 Answers 2

9
+50
$\begingroup$

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 : www.wolfram.com

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

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

\newcounter{mathematicapage}
\begin{document}

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

\end{document}

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.

CreatePalette[DynamicModule[{},
  Button["To LaTeX", copyToClipboard[
    StringTrim@
     StringReplace[
      ExportString[NotebookRead[SelectedNotebook[]], 
       "LaTeX"], __ ~~ "begin{document}" ~~ text__ ~~ 
        "\\end{document}" ~~ ___ :> text]]],
  Initialization :> (copyToClipboard[text_] := 
           Module[{nb},
             nb = NotebookCreate[Visible -> False];
             NotebookWrite[nb, Cell[text, "Text"]];
             SelectionMove[nb, All, Notebook];
             FrontEndTokenExecute[nb, "Copy"];
             NotebookClose[nb];
           ])
  ]
 ]

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!

$\endgroup$
7
  • $\begingroup$ @M.R. I would love to get some feedback from you. Is it working? $\endgroup$
    – halirutan
    Commented Nov 26, 2012 at 17:31
  • $\begingroup$ 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! $\endgroup$
    – M.R.
    Commented Nov 26, 2012 at 18:19
  • $\begingroup$ Do you know how to add arbitrary commands into a item in KeyEventTranslations.tr? $\endgroup$
    – M.R.
    Commented Nov 26, 2012 at 18:19
  • $\begingroup$ Just search for KeyEvent on this site. We had various question regarding custom commands. Look for instance here. $\endgroup$
    – halirutan
    Commented Nov 26, 2012 at 18:33
  • $\begingroup$ I know about them, but the examples I have seen only use FETokens and never execute arbitrary code. $\endgroup$
    – M.R.
    Commented Nov 26, 2012 at 18:38
4
$\begingroup$

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:

img1

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:

img2

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.

$\endgroup$
2
  • $\begingroup$ Thanks @silvia for your solution, but I feel that the other one is simpler... $\endgroup$
    – M.R.
    Commented Nov 26, 2012 at 18:50
  • $\begingroup$ @M.R. of course~ I absolutely agree with you :) $\endgroup$
    – Silvia
    Commented Nov 27, 2012 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.