3
$\begingroup$

Suppose we have a cell like this:

boxes = Cell[
   BoxData[RowBox[{"foo", "[", "\[IndentingNewLine]", 
      RowBox[{RowBox[{"{", 
          RowBox[{"1", ",", "2", ",", "3", ",", "4", ",", "5", ",", 
            "6", ",", "7", ",", "8", ",", "9", ",", "10", ",", "11", 
            ",", "12", ",", "13", ",", "14", ",", "15", ",", "16", 
            ",", "17", ",", "18", ",", "19", ",", "20", ",", "21", 
            ",", "22", ",", "23", ",", "24", ",", "25"}], "}"}], ",", 
        "\[IndentingNewLine]", "\"asd\""}], "\[IndentingNewLine]", 
      "]"}]], "Input"];

To convert it to interpretable ASCII text, we can use

First @ FrontEndExecute @ FrontEnd`ExportPacket[boxes, "InputText"]

This produces

foo[
 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
  20, 21, 22, 23, 24, 25},
 "asd"
 ]

Notice that the code was wrapped at a width of about 70 characters. How can I control this wrapping width?

"What have you tried?"

ExportPacket[..., "InputText", PageWidth -> 200] does not change the wrapping width.

Setting these does not work:

enter image description here

Possibly related:

| improve this question | | | | |
$\endgroup$
  • 1
    $\begingroup$ That can not be controlled from user land. $\endgroup$ – ihojnicki Jan 31 at 20:47
  • $\begingroup$ @ihojnicki Is there any other way to convert an input cell's content to relatively nicely formatted plain text, and not wrap it at 70 characters? It doesn't have to look exactly the same as "InputText", but some spacing around operators would be nice. $\endgroup$ – Szabolcs Jan 31 at 21:51
  • 1
    $\begingroup$ Off of the top of my head? Not that I can recall. But when I get a chance, I'll look. $\endgroup$ – ihojnicki Jan 31 at 22:10
  • $\begingroup$ @ihojnicki For context, it's for converting notebooks to markdown and preserving a relatively readable formatting. Here's the current version: szhorvat.net/mathematica/IGDocumentation $\endgroup$ – Szabolcs Feb 2 at 16:17
3
$\begingroup$

Here is a trick to make it infinite:

  1. mark all existing new lines
  2. convert to text
  3. remove marks and not marked new lines
ToInfWidthInputText[boxes_] := Module[
  {tagged, mark = "ORYGINALMARK", iNL = FromCharacterCode@{62371}}
  ,
  tagged = boxes /. (n : "\n" | iNL) :> mark <> n;
  tagged = First @ FrontEndExecute @  FrontEnd`ExportPacket[tagged, "InputText"];

  StringReplace[
   tagged,
    {
      (mark ~~ "\r"...~~"\n") :> "\n",
      "\\"~~"\r"...~~"\n" -> "", (* in strings *)
      ("\r"...) ~~ "\n" ~~ " " ... -> ""
    }
   ]
  ]
    ToInfWidthInputText @ NotebookRead @ PreviousCell[]

 ToInfWidthInputText[boxes_] := Module[
   {tagged, mark = "ORYGINALMARK", iNL = FromCharacterCode@{62371}} 
   , 
    tagged = boxes /. (n : "\n" | iNL) :> mark <> n; 
    tagged = First@FrontEndExecute@FrontEnd`ExportPacket[tagged, "InputText"]; 

    StringReplace[
     tagged, 
     {
      (mark ~~ "\r" ... ~~ "\n") :> "\n", 
      "\\" ~~ "\r" ... ~~ "\n" -> "",(*in strings*)
      ("\r" ...) ~~ "\n" ~~ " " ... -> "" 
     } 
    ] 
  ]
| improve this answer | | | | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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