Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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"]
share|improve this question
3  
Just to get the correct string before programming anything: I guess you're missing \( before the second M and \) after the following A. –  einbandi Nov 20 '12 at 15:21
    
I pasted it back in. It returns $Aborted. –  M.R. Nov 20 '12 at 20:59
    
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[System`FEDump`TransformBoxesToTraditionalFormBoxes[System`FEDump`processBoxe‌​sForCopyAsTeX[{TextForm},TextData[#1]]]&,<<14>>, if there is a TM that accepts it.] is not supported. >> –  M.R. Nov 20 '12 at 21:04
add comment

2 Answers 2

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!

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

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.

share|improve this answer
    
Thanks @silvia for your solution, but I feel that the other one is simpler... –  M.R. Nov 26 '12 at 18:50
    
@M.R. of course~ I absolutely agree with you :) –  Silvia Nov 27 '12 at 15:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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