Group Abstract Group Abstract

Message Boards Message Boards

1
|
12.6K Views
|
11 Replies
|
9 Total Likes
View groups...
Share
Share this post:

Output as ordered pair (5, -2)?

Posted 9 years ago

Let's say I have a list of pairs of numbers, like this:

pts={{4,7},{-5,1/4},{7/12,6},{-1,0}};

I want to output individual points in the form of (4,7), possibly with a space after the comma. This is how students in the US usually see points formatted. So far, the only way I have been able to do it is by building up a string for each point, like this:

"(" <> ToString[#[[1]]] <> ", " <> ToString[#[[2]]] <> ")" & /@ pts

But there are a few problems with this method. First, it doesn't work for fractions, though a more elaborate version might. Second, converting numbers to strings isn't really consistent with the symbolic nature of Wolfram programming.

How should I get the points to output as traditional ordered pairs?

Thanks in advance,

Mark Greenberg

POSTED BY: Mark Greenberg
11 Replies
Posted 9 years ago

Ah, very nice! I suspected that there was a way to do it, and there is. I have seen in the docs that box functions exist, but I assumed that they had something to do with input and output containers. I didn't imagine they were the solution to my question, and they appear to be pretty simple too. Thank you very much, Sander! : )

POSTED BY: Mark Greenberg

There are generally many ways to achieve something in Mathematica/Wolfram Language, without knowing what someone REALLY wants to do and all the cases they consider and what 'fringe-cases' it should support, et cetera the answer can vary... hope the above works for you!

POSTED BY: Sander Huisman
CreateVector2[x_List] := Row[Join[{"("}, Riffle[x, ","], {")"}]]
CreateVector2 /@ {{1, 2}, {3/4, a}, {\[Pi], Graphics[{Red, Disk[]}]}}

Now you can use anything: numbers, symbols, graphics, networks...

POSTED BY: Sander Huisman
Posted 9 years ago

Yes, this is closer to what I have in mind, only the parentheses don't scale to their content. Notice the second point especially in this example. Can one indicate "stretchy" parentheses in the Wolfram Language, as is possible in LaTex, or cause parentheses to automatically resize to their content as happens in equation editors and in Wolfram notebooks in other contexts?

enter image description here

POSTED BY: Mark Greenberg

I'm not sure you can explicitly introduce stretched parenthesis easily... That could be tricky to do I think! Though TraditionalForm does make stretched parenthesis:

enter image description here

Alternatively, you could use the small package MaTeX

http://szhorvat.net/pelican/latex-typesetting-in-mathematica.html

to get true latex into Mathematica...

POSTED BY: Sander Huisman
Posted 9 years ago

Two good suggestions. Thank you, Sander.

Unfortunately, TraditionalForm uses stretched parentheses only in places where Wolfram syntax says that parentheses are needed, as in your example in which the parentheses group the disc and the fraction for the exponent. It doesn't recognize (2,7) as a valid syntax, so it throws an error. It would be nice if the TraditionalForm of the correct syntax for a point, Point[{2,7}], would format as (2, 7), but it only outputs Point[{2,7}] in "Times" font.

I did look over the MaTex site, which looks like it is too involved for my needs, but that may be the only way to achieve what I am after.

Thanks for the help. : )

POSTED BY: Mark Greenberg

This one uses some low-level tricks to make it work:

CreateVector3[x_List]:=DisplayForm[StyleBox[RowBox[{"(",Row[x,","],")"}],SpanMaxSize->Infinity]]
CreateVector3/@{{1,2},{3/4,a},{\[Pi],Graphics[{Red,Disk[]}]}}

enter image description here

Using the low-level function XYZBox you can typeset anything and using AdjustmentBox you can tune it further...

POSTED BY: Sander Huisman
Posted 9 years ago

Thank you, Wilson and Sander, for showing me more elegant ways of converting point coordinates to strings. It's cool how there are so many ways to do the same thing in the Wolfram Language.

I'm still wondering whether there is a way to keep a point as a symbol while outputting its coordinates separated by a comma and surrounded by parentheses. Disadvantages of a string approach include non-scaling parentheses and fractions that look like 7/12 rather than vertically stacked fraction.

Any further suggestions out there?

POSTED BY: Mark Greenberg

Hi Mark,

You could use StringRiffle so you can use the following functions for any length vector:

CreateVector[x_List]:=StringRiffle[ToString[#,InputForm]&/@x,{"(",",",")"}]
pts={{4,7},{-5,1/4},{7/12,6},{-1,0}};
CreateVector/@pts

I think this is quite elegant code. Not so bad. Of course you could also take the StringTemplate route:

StringTemplate["(`1`, `2`)"] @@@ pts

also not too bad (though 'slower' I think). But probably that is not of your concern.

POSTED BY: Sander Huisman
Posted 9 years ago

Hi Mark,

You can use one of the options in ToString to solve the format issue with fractions:

"(" <> ToString[#[[1]], InputForm] <> ", " <> ToString[#[[2]], InputForm] <> ")" & /@ pts

I would like to know if there's a nicer way as well to not convert things to string to display with round brackets.

Cheers,

Wilson

POSTED BY: Wilson Kan
Posted 9 years ago

I still got the 7/12 version of the fraction with this code:

enter image description here

POSTED BY: Mark Greenberg
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard