5

I have just been working through this splendid answer to my question.

I would like to copy the words as well as the code into a Mathematica notebook. I usually do this by putting the words in a text cell (Alt 7). However, there are i) equations that don't copy and ii) this is laborious.

Is there a way of copying the whole answer and getting into a notebook easily?

3
  • Unfortunately, the answers are not notebooks and it is likely you can’t automate conversion of text/LaTeX to notebook. Obviously, code is copyable. Good luck. Commented Jan 22 at 9:10
  • 1
    Have you tried this: mathematica.stackexchange.com/a/3537/4999 Commented Jan 24 at 0:16
  • @MichaelE2 Thanks for the link very useful. Works well. Commented Jan 26 at 8:46

1 Answer 1

6

In version 14.3, Mathematica got a functionality to import Markdown. Since all answers in StackExchange are written using Markdown, this is a simple way to import them to your Mathematica notebook.

Here is a simple snippet, which uses StackExchange Data API to obtain the contents of the answer. There is also a small fix such that all code blocks are by default treated as Mathematica code (otherwise, they will not be rendered as Input cells).

importStackExchangeAnswer[answerid_] := Module[{url, json, markdown},
  url = "https://api.stackexchange.com/2.3/answers/" <> ToString[answerid] <> 
    "?site=mathematica&filter=!-)QWsboN0d_T";
  json = Import[url, "JSON"];

  markdown = ResourceFunction["DecodeHTMLCharacterEntities"][
    Lookup[First[Lookup[json, "items"]], "body_markdown"]];
  
  Internal`InheritedBlock[{MarkdownTools`Private`getWithDefault},
   MarkdownTools`Private`getWithDefault[l_List, "Language", ""] := 
    MarkdownTools`Private`getWithDefault[l, "Language", "WL"];
   
   ImportString[markdown, {"Markdown", "NotebookObject"}]
   ]
  ]

From the URL, you obtain the ID of the answer and then evaluate

importStackExchangeAnswer[318477]

enter image description here

6
  • Interesting approach. Thanks. But I tried it on 2 answers from Mathematica (including the one you show) and it gives these error for me, using V 14.3 on windows 10. i.sstatic.net/0bMOVaHC.png Commented Jan 23 at 21:21
  • fyi, finally got it to work. I needed to install Python on windows 10 and also install pyzmq. For some reading this, if you do not have Python on windows do the following, From CMD terminal, just type python with no arguments., This will install it from app store. Once installed, then type pip install pyzmq also from terminal. Now it works. Commented Jan 23 at 21:48
  • fyi, I just noticed you hardcoded the site name inside your module. If will be useful to make the stackexchange site name as an argument as well. So this can be used for other sites and not just Mathematica. User will have to pass in the site name as string but that is ok,. Commented Jan 23 at 22:04
  • I found another small issue. Some results from the command json = Import[url, "JSON"] do not have item called body_markdown, so this is why this command Lookup[First[Lookup[json, "items"]], "body_markdown"] can fail sometimes. You might want to add a check if body_markdown is present before calling ResourceFunction, else it generate an error. I do not know anything about this stackexchange API, I just found this trying your code on some answers and question and some fail due to missing body_markdown Commented Jan 23 at 22:17
  • Very instructive. Thank you @Nasser for other information Commented Jan 24 at 2:45
  • Thanks but I don't have the other needed software. I will have to consider this. Commented Jan 26 at 8:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.