The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Hot answers tagged

54

Have a look at FileNames: files=FileNames["*.pdf", NotebookDirectory[]] {"a.pdf","b.pdf","c.pdf"} will get you a list of all files in the directory where your notebook resides (of course you can choose any path) that match "*.pdf". You can then import the files like this: Import[#]&/@files or if you want certain files (look at the help for Part ...


36

Assuming nb is your notebook object, then this will do what you want without touching the clipboard: First[FrontEndExecute[ FrontEnd`ExportPacket[NotebookSelection[nb], "InputText"]]] Some notes about this solution: It preserves evaluation semantics precisely, regardless of typesetting. It does not dirty the clipboard If you prefer to get the appearance ...


35

Code extractor using the StackExchange API The following code uses the 2.0 version of the SE API and has also been cleaned up a bit (place it in your kernel's init.m or your custom functions package if you'd like to be able to use it anytime). The function takes a single string argument, which is the URL obtained from the share link under a question/answer....


31

Import text as a FilledCurve in graphics, using PDF as an intermediate format. Below are modified examples from Documentation Center: text = First[First[ImportString[ExportString[Style["Hi", Italic, FontSize -> 24, FontFamily -> "Times"], "PDF"], "PDF", "TextMode" -> "Outlines"]]]; Outline fonts using different edge and face forms: Graphics[{...


28

You can use Inset: Show[{Graphics3D[{Opacity[0.2], Sphere[], Opacity[1.0], Blue, Inset[Graphics[Text[Style["Surprise!", Green, 24]]], {0, 0, 0}]}], ParametricPlot3D[{Sin[th] Cos[ph], Sin[th] Sin[ph], Cos[th]}, {th, 0, Pi}, {ph, 0, 2 Pi}, RegionFunction -> Function[{x, y, z}, Abs[x] < .9], PlotRange -> {-1, 1}, PlotStyle -> ...


23

For this purpose I made a function that puts an arbitrary expression into a 3D graphic. It's described on this page, going back originally to this MathGroup post, I'll copy the code here: label3D[s_, pos_, xVec_, tiltAngle_, opts : OptionsPattern[]] := Module[{ra, width, height, r}, ra = Rasterize[ Style[HoldForm[s], FilterRules[{opts}, Options[...


23

The following response borrows shamelessly from Mr.Wizard: Manipulate[ Graphics[{{Dashed, If[circle, Circle[{0, 0}, r], {}]}, Rotate[MapThread[ Rotate[Text[Style[#, FontFamily -> "Courier", fs], #2], 90° - #3] &, {txt, {-r Cos[#], r Sin[#]} & /@ (range = Range[0, arc, arc/(Length@txt - 1)]), range}], θ, {0, 0}]}, ...


22

This places a string on the outside of a unit circle. It works for variable width fonts. circularText[str_, ang : {a0_, a1_} : {0, 2 Pi}, scale:(_?NumericQ): 1] := Module[{text, curves, pts, xrange, ymin, xrlst, subgroups, maxwidth, centers}, (* transform string to FilledCurves *) text = ImportString[ ExportString[Style[str, Bold, FontFamily -&...


21

An alternative approach that does not require the ComputationalGeometry package: You can use a combination of ClusteringComponents and ComponentMeasurements as follows: Define chVertices[txtimg_] := Insert[#, First@#, -1] &@ ComponentMeasurements[ClusteringComponents[Binarize@txtimg, 2], "ConvexVertices"][[2, 2]] Example data: textimg1 = ...


21

This is just a quick sketching out of an answer (rescales galore!) textOnCurve[text_, f_, n_, p_: 0.01] := Text[Rotate[text, ArcTan @@ (f[Rescale[n + p, {0, 1}, {p, 1 - p}]] - f[Rescale[n - p, {0, 1}, {p, 1 - p}]])], f[n]] textCurve[string_, f_, stylef_: (# &), range_: {0, 1}] := With[{chars = Characters@...


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, ...


20

It might be overkill, but with: DictionaryLookup[{"German", All}] you get a list of every German word. To be a bit more precise, you could then narrow things with string searches, i.e. DictionaryLookup[{"German", "a" ~~ ___ ~~ "b"}] gets you a list of words that begin with a and end with b. Full documentation is here.


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 ...


19

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"...


18

Here's another way...Text[] has a direction argument, so ArcTan is not necessary. txt1 = "Now we can follow" // Characters; txt2 = "an arbitrary path" // Characters; f[t_] := {Cos[2 π t], Sin[6 π t]}; totalarclength = NIntegrate[Sqrt[f'[τ].f'[τ]], {τ, 0, 1}]; invarclength = First@NDSolve[{D[$t[s], s] == 1/Sqrt[f'[$t[s]].f'[$t[s]]], $t[0] == 0}, $t, {s, 0, ...


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 ...


17

You could use Row to build up the text to be shown: aboveBox[info_, colors_] := Graphics[{colors, EdgeForm[Thick], Rectangle[{0, 0}, {26, 3}], Text[Row[{Style["subject", 12, Bold, Black, TextAlignment -> Center], Style[info, 18, Bold, Red, TextAlignment -> Center]}], {26, 3}/ 2]}, ImageSize -> 300] aboveBox["AK6", ...


17

You could do something like this: string = "(Paste Here)" exps = Select[ string ~StringSplit~ "\n\n", SyntaxQ@# && ! MatchQ[MakeExpression@#, _@__Times | _@Null] &]; CellPrint@Cell[#, "Input"] & ~Scan~ exps


17

You can open a text cell using Alt7, and type some text. When you need math, open a subcell using Ctrl9. Single letter variables will be formatted in italics in the subcell, as is usual. Use the usual shortcuts (e.g. Ctrl6 for superscript) to enter math. Use CtrlSpace to get out of the subcell (or the arrow keys). On a Mac, use ⌘ in place of Alt.


17

Here is a starting point: txt = "This is some text to warp." // Characters; arc = 1; range = Range[0, arc, arc/(Length@txt - 1)]; coords = {-Cos[#], Sin[#]} & /@ range; Graphics[ MapThread[ Rotate[Text[Style[#, FontFamily -> "Courier"], #2], 90° - #3] &, {txt, coords, range}] ]


16

You can generate actual 3D data describing the text by Importing from PDF. wordData = ImportString[ExportString["Surprise", "PDF"], "PDF"][[1, 1, 2, 1, 1, 2]]; Graphics3D[Tube[#, 0.2] & /@ Map[Append[#, 0] &, wordData, {2}]] Or, in reference to Sjoerd's comment to the OP, wordData = ImportString[ExportString[Style["\[Euro]", FontFamily -> ...


15

There is a subtle problem if you use strings as axis labels. Look closely at a plot like this: Plot[f, {x, -1, 1}, AxesLabel -> {x, "\!\(\*SubscriptBox[\(f\), \(i\)]\)(x)"}] You will see that the argument x in the function appears in a different font style than the argument on the horizontal axis. To make sure that you get a consistent font style on ...


15

The question could be rephrased: how do we get a vector graphic from a bitmap? The solution is pretty simple by using the code we can find here. p = Image[Graphics[Text[Style["Get Convex Hull points.", Large]]]]; img = Thinning@EdgeDetect@p; points = N@Position[ImageData[img], 1]; pts = Union@Flatten[FindCurvePath[points] /. c_Integer :> points[[c]], 1]; ...


15

Per Vitaliy's request: With[{before = 4, after = 5}, StringTake[text, First[StringPosition[text, "entitle"]] + {-before, after}]] yields "God entitle them".


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

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

Modifying an example from the help on FilledCurve[]: Module[{l = Cases[First[First[ImportString[ExportString[Style["CIRCLE", Bold, FontFamily -> "Courier", FontSize -> 12], "PDF"], "TextMode" -> "Outlines"]]], FilledCurve[a__] :> {EdgeForm[Black], Yellow, FilledCurve[a]}, Infinity]}, Animate[Graphics[{Red, Circle[{0, 0}, 1.5],...


14

In the answer linked here, I did the following: out = FileNameJoin @ {$TemporaryDirectory, "MathematicaOutput" <> ToString /@ Date[] <> ".rtf"} /tmp/MathematicaOutput2012519111731.900549.rtf Then you would say Export[out, ...]. If you want to have the date in a more readable and less detailed form, you could use this for the name: "file-" &...


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