6
$\begingroup$

Maybe this is something very simple to some of you. Anyway, I think this can be useful to many others like me.

I would like to build an output that involves strings, unevaluated expressions and evaluated expressions/values —something like this:

The value for $C_\mathrm{p}$ is $1.5$ and the value for $\delta^*$ is $0.123$.

where 1.5 and 0.123 are the result of evaluating respective expressions.

This is what I tried:

Cp = 1.5; deltastar = 0.123;
HoldForm["The value for " HoldForm[Subscript[C, p]] " is " Evaluate[
   Cp] " and the value for " HoldForm[
   Superscript[\[Delta], "*"]] " is " Evaluate[deltastar] "."]

With this result:

enter image description here

The outer HoldForm is necessary to maintain the order of the pieces, that are actually considered by Mathematica as being multiplied. For instance, this piece of code:

Cp = 1.5; deltastar = 0.123;
"The value for " HoldForm[Subscript[C, p]] " is " Evaluate[
   Cp] " and the value for " HoldForm[
   Superscript[\[Delta], "*"]] " is " Evaluate[deltastar] "."

yields to this output:

enter image description here

Using ToString is not an option when there are superscripts and subscripts, as it is the case (I mean, the raw string output is awful). And because of that, I can't use string concatenation 😕, which would be the most natural approach to me.

There should be an easy way to do this in Mathematica, shouldn't there?

As I said, I guess there actually is a simple solution for this —that is, properly building an output text or message that involves strings, unevaluated expressions and evaluated expressions. Could you help me, please?

$\endgroup$
7
$\begingroup$

It sounds like you're merely looking for Row:

Cp = 1.5; deltastar = 0.123;

Row[{
 "The value for ",
 HoldForm[Subscript[C, p]],
 " is ",
 Cp,
 " and the value for ",
 HoldForm[Superscript[\[Delta], "*"]],
 " is ",
 deltastar,
 "."
}]

enter image description here

If this does not work for you please clearly state how it fails so that those issues can be directly addressed.

$\endgroup$
7
$\begingroup$

Looks like StringForm can achieve this:

Cp = 1.5; deltastar = 0.123;

Then:

StringForm["The value for `1` is `2` and the value for `3` is `4`.", 
                HoldForm @ Subscript[C, p], Cp, HoldForm @ Superscript[\[Delta], "*"], deltastar]

Mathematica graphics

$\endgroup$
  • 1
    $\begingroup$ It is worth noting that NumberMarks are displayed with this method. (e.g. "1.5`") Using NumberMarks -> False does not seem to change this. $\endgroup$ – Mr.Wizard Sep 12 '14 at 13:42
3
$\begingroup$

In V10 there is a new function StringTemplate that allows us to build custom formatting functions in a new way. Here is how it can applied to the OP's problem.

fmt[args__] := 
  Style[
    StringTemplate[
      "The value for `1` is `2` and the value for `3` is `4`.", 
      CombinerFunction -> Row
      ][args], 
    "SR"]

cpForm = HoldForm@Subscript[C, p]; Cp = 1.5;  
deltaForm =  HoldForm[Superscript[δ, "*"]]; deltastar = 0.123;
fmt[cpForm, Cp, deltaForm, deltastar]

fmt-1

Cp = 1.3; deltastar = 0.42;
fmt[cpForm, Cp, deltaForm, deltastar]

fmt-2

Note that the TemplateObject returned by StringTemplate acts like a function.

$\endgroup$
2
$\begingroup$
a = 1.5;
b = 0.123;

Grid[{{"The value for", RawBoxes[SubscriptBox["c", "p"]], "is", a,
   "and the value for", RawBoxes[SuperscriptBox["\[Delta]", "*"]], "is", b}}]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ +1 but I think Row is easier to use an more "canonical" here. $\endgroup$ – Mr.Wizard Sep 12 '14 at 11:20

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.