Hot answers tagged

21

Here's a method based on creating a MeshRegion from the text: text = Style[HoldForm @ Sum[x^2, {x, 0, 10}], 100, Bold]; graphics = First[text ~ExportString~ "PDF" ~ImportString~ "PDF"]; region = DiscretizeGraphics[graphics, MaxCellMeasure -> 5]; image = ExampleData[{"ColorTexture", "Kingwood"}]; RegionPlot[region, Frame -> False, ...


21

You want to use an UnderBrace. Highlight the g....g then type Ctrl-4 to get under it, then type Esc u{ Esc, then highlight the underbracket and hit Ctrl-4 again, gives this To get better, you might want to use Szabolcs's MaTeX package, in which case you will get an image of the rendered TeX, <<MaTeX` MaTeX@Underscript["g...g", Underscript[︸, "n-times"...


20

I have this palette open all the time: CreatePalette@Row@ { Button["(\[SelectionPlaceholder])", FrontEndExecute[ FrontEndToken[SelectedNotebook[], "SelectionParenthesize"]]], Button["[\[SelectionPlaceholder]]", FrontEndExecute[ FrontEndToken[SelectedNotebook[], "SelectionBracket"]]], Button["{\[...


19

Here's an attempt to implement the idea suggested by Szabolcs, based on Heike's clever image processing method. The code uses MinFilter to identify regions where the label may go without overlapping anything, and Nearest to pick the closest point to the desired position. The rest is just scaling between image and graphics coordinates. It won't be fast ...


18

Here is an approach that handles interspersed comments in addition to "headers" FilePrint["test.txt"] #comment #comment #comment 1 2 3 #c2 4 5 6 7 8 9 ImportString[ StringReplace[Import["test.txt", "Text"], StartOfLine ~~ "#" ~~ Shortest[___] ~~ EndOfLine ~~ "\n" -> ""], "Table"] {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} of course you can ...


15

Lots of answers, but none of them leveraging this, so here is another. null[_String] := Null Length @ ReadList["data.txt", null @ String, NullRecords -> True] On my system this is more than three times as fast as Rolf Mertig's CountLines, and a lot more concise as well. If even one Null for every record is too much memory usage then read in blocks of ...


15

The Import command supports an option to ignore header lines. In many cases this is the easiest solution. For example: dataStats = Import["C:/data/stats.csv", "CSV", HeaderLines -> 4];


15

The answer is in the docs yet it might be useful for others so I will just drop it here. The Details and Options section of the Text documentation says: Thus you can do the following: Plot[x, {x, 0, 1}, Epilog -> {Text[Style["hello", 25], Scaled[{0.5, 0.5}], #], Red, Point@{.5, .5}}, PlotLabel -> ToString@#] & /@ {{-1, 0}, {1, 0}, {0, ...


15

A little spelunking of the code for DeleteStopwords[] yields the internally used stopword list: DeleteStopwords; (* force auto-load *) AlphabeticSort[List @@ TextProcessing`TextModificationDump`$stopWords["English"]] // Short {"a", "A", "about", "above", "across", "after", "again", "against", "all", "almost", "alone", "along", "already", "also", "...


14

I posted a possible answer for this on meta (Download questions or chats for offline reading), but perhaps it belongs here on the main site instead. I have a paclet that downloads a stack exchange question url, and creates a notebook version where code blocks are evaluatable. It can be downloaded from: https://github.com/carlwoll/Stack-Exchange-Stylesheet/...


14

I believe this is the documentation you are looking for: String Representation of Boxes Notably: And:


14

The stop-words are documented and can be looked up as (see Details section of WordList): WordList["Stopwords"] As it might be subject to change so the output below can get outdated, but you can always run the function WordList["Stopwords"]: {"0","1","2","3","4","5","6","7","8","9","a","A","about","above","across","after","again","against","all","almost",...


14

Here is an approach using some basics of object-oriented programming. Load the lyrics: str = "It's been a long, long time\nSince I've memorized your face\nIt's been four hours now\nSince I've wandered through your place\nAnd when I sleep on your couch\nI feel very safe\nAnd when you bring the blankets\nI cover up my face\nI do Love you, I doLove you\...


13

My solution, which does not try to place the letters, but rather transform the space they inhabit. This has an extra feature/bug/property that parallel lines in the letters may no longer be parallel after the transformation. WrapText[str_, transformation_] := Module[{text}, text = Style[str, Bold, FontFamily -> "Helvetica", FontSize -> 12, ...


13

A simple Mathematica-only solution is: CountLines[file_String /; FileExistsQ[file]] := Module[{counter = 0, str = OpenRead@file}, While[ Read[str, Record, NullRecords -> True] =!= EndOfFile, counter++ ]; Close[str]; counter]; which is quite slow of course, so 123 MB (1978142 lines) ...


13

Extract all Greek letters from the documentation and make replacement rules: nb = Get @ FileNameJoin[{$InstallationDirectory, "Documentation", "English", "System", "Tutorials", "LettersAndLetterLikeForms.nb"}]; letters = Cases[nb, StyleBox[s_String, "TR"] :> s, {-2}]; letters = DeleteCases[letters, "π" | "∈"]; (* reserved Symbols *) names = ...


13

1. ROT13 rotN[str_String, n_Integer: 13] := Module[ { rule = Flatten@Normal[ AssociationThread[#, RotateRight[#, n]] & /@ { CharacterRange["a", "z"], CharacterRange["A", "Z"] } ] } , StringReplace[str, rule] ] Example rotN["Mathematica Stackexchange, Zngurzngvpn Fgnpxrkpunatr"] "Zngurzngvpn ...


12

The simplest and most efficient way would probably be using the common wc external utility. For example, In[33]:= Import["!wc ~/test.m", "Table"] Out[33]= {{6, 5, 56, "/Users/szhorvat/test.m"}} You'll get wc by default on Linux/OSX, but you can install it on Windows too.


12

(J.M.'s comment) You can select a cell and do Format > Style > Text, or you can directly right-click on a cell bracket, or you can convert or generate a new text cell by the keyboard shortcut Alt+7. Also, in version 10, you should see a popup prompting you to convert your cell into a text cell when you start typing something that looks like a sentence, ...


12

Perhaps something like this? Animate[ Graphics[ { FaceForm[White] , EdgeForm[{Thick, Black}] , {Gray, Line[{{-12, 0}, {12, 0}}]} , Disk[{t, 0}, 2] , Text[Style[If[t < 0, "|0〉", "|1〉"], 32], {t, 0}] , Rectangle[{-3, -3}, {3, 3}] , Text[Style["X", 64], {0, 0}] } , PlotRange -> {{-12, 12}, Automatic} ] , {t, -10, 10} ...


12

Mathematica allows text searching using regular expressions (based on the PCRE library). It would take some work to re-implement the whole grep functionality within Mathematica, but for your concrete example grep -nr -C 2 <pattern> it is as easy as follows: ClearAll[Grep] Grep[files_List, patt_, c_Integer: 0, style : {__} : {Red, Bold}] := ...


11

Setting a FontOpacity less than 1 appears to prevent sub-pixel rendering and therefore provides output similar (perhaps identical) to that obtained with ClearType switched off. text = Style["αβημπρτ", 20, FontFamily -> "Arial", FontOpacity -> 0.999]; Rotate[text, # °] & /@ {90, 90.1, -90, 85, 95, 45, 0} One possibility to automate this, and to ...


11

I have had some success with the following function that I wrote a couple of years ago to battle quoted printables on MathGroup: translateQuotedPrintable[str_String] := StringReplace[str, {"=" ~~ c1:HexadecimalCharacter~~c2:HexadecimalCharacter :> FromCharacterCode[FromDigits[c1 <> c2, 16], "Math1"],"=" ~~ EndOfLine -> ""}] ...


11

The Program style is purpose-built for this sort of thing: If you do not like the look of the bars above and below the cell, you can remove them by selecting the cell and changing the CellFrame option in the Option Inspector:


11

There are two alternatives I would suggest, depending on what your plans for the Background are. Here is an illustration: Text[Pane[Style["(1, 0, 0)", 12, Background -> Yellow], ImageMargins -> 10], {1, 1, 0}] Text[Framed[Style["(1, 0, 0)", 12], Background -> Yellow, FrameMargins -> 10, FrameStyle -> None], {1, 1, 0}] The first ...


11

There is another way that is on my machine almost 500x faster then your solution. The idea is to look how Mathematica represents colored strings and use this directly. When we colorize an input string by selecting text and using the Format menu, we can create something like this Now, press Ctrl+Shift+E to see the underlying expression. Cell[BoxData["\"...


11

You can see the zero-width character using FullForm: str //FullForm "\:200b\:200b4-6" So, in this case, you can use the following: StringReplace[str, "​\:200b"->""] //FullForm "4-6" Update Added a threshold, default 2, for determining "zero" width characters You can use the following function to look for zero-width characters in your string: ...


10

You can use Word's "Convert Text to Table" function for this. Select the grid in Mathematica and copy as plain text. Paste it into Word, select it and convert to a table You get this:


10

What a strange idea! You could play with this approach: rainbow[l_List] := (l /. {"1" -> Style["1", 18, Red], "2" -> Style["2", 18, Blue], "3" -> Style["3", 18, Purple], "4" -> Style["4", 18, Cyan, Background -> Black] (* and so on *) }) format[i_Integer] := Row[Join[rainbow[ToString /@ IntegerDigits[i]]]]; $Post = format; Now your ...


Only top voted, non community-wiki answers of a minimum length are eligible