3
$\begingroup$

I want to convert an integer number fraction as text with a certain font, size and color:

Example:

x = {0, 2, 3, 0, 4};
y = {4, 5, 12, 10, 1};

Style[ToString[#, TraditionalForm], FontFamily -> "Calibri", 20, 
   Red] & /@ (x/y)

The result is:

enter image description here

I would like to see the fraction values {0/4, 2/5, 3/12, 0/1, 4/1} and not the results.

$\endgroup$
4
  • 1
    $\begingroup$ One quick way is to replace 0 with "0". $\endgroup$ Feb 18, 2019 at 10:30
  • 2
    $\begingroup$ What is allowed to be simplified? E.g. you don't complain about 3/12. $\endgroup$
    – Kuba
    Feb 18, 2019 at 10:34
  • $\begingroup$ Closely related: mathematica.stackexchange.com/q/71938/5478 $\endgroup$
    – Kuba
    Feb 18, 2019 at 10:35
  • $\begingroup$ @Kuba: I would like to see the fraction values and not the results -> {0/4, 2/5, 3/12, 0/1, 4/1} $\endgroup$
    – lio
    Feb 18, 2019 at 10:36

2 Answers 2

5
$\begingroup$
Style[ToString[#, TraditionalForm], FontFamily -> "Calibri", 20, Red] & /@ 
  (HoldForm /@ x / HoldForm /@ y)

enter image description here

Also

Map[Style[ToString[#, TraditionalForm], FontFamily -> "Calibri", 20, Red] &, 
  Inactivate[Divide[x, y]],  {-1}] // Activate

same picture

and

style = Style[ToString[#, TraditionalForm], FontFamily -> "Calibri", 20, Red] &;
(style /@ x )/(style /@ y)

same picture

Update: You can use

MapThread[Style[Row[{##}, "/"], FontFamily -> "Calibri", 20, Red] &, {x, y}]

or

Style[ToString[#, TraditionalForm], FontFamily -> "Calibri", 20, Red] & /@ 
  MapThread[Row[{##}, "/"] &, {x, y}]

to get

enter image description here

$\endgroup$
4
  • $\begingroup$ This is perfect. Due to space problems in my 2d plot (the fractions are used as labels for certain points and are vertically too close to each other) I would be interested to use them as a string like this: 2/5. $\endgroup$
    – lio
    Feb 18, 2019 at 10:55
  • $\begingroup$ @lio, please see the update. $\endgroup$
    – kglr
    Feb 18, 2019 at 11:14
  • $\begingroup$ Thank you so much ... $\endgroup$
    – lio
    Feb 18, 2019 at 11:27
  • $\begingroup$ @lio, my pleasure. $\endgroup$
    – kglr
    Feb 18, 2019 at 11:27
3
$\begingroup$

You could create a wrapper that formats as desired:

MakeBoxes[HoldRational[n_Integer, d_Integer], StandardForm] ^:= MakeBoxes[
    Style[InputForm[Divide[n, d]], FontFamily->"Calibri", FontColor->Red, 24],
    StandardForm
]

Then:

x = {0, 2, 3, 0, 4};
y = {4, 5, 12, 10, 1};

MapThread[HoldRational, {x, y}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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