You may have a database field which contains code containing characters that you would like to convert to / print as subscript or superscript. One workaround has been to just paste the superscript or subscript character on top of an existing text object. This may be fine for static layouts but doesn’t work at all for changing data.

Below is a formula which will convert the code in your database to proper subscript or superscript in your output. This assumes that you are using a font which can actually handle those characters. Arial Unicode MS is an example of a font that will print those unicode subranges properly. Please note the characters in the formula (look at the sections with “replace”) that will be converted for both subscript and superscript.

To use this formula for your data, change the s stringvar to your field. Then change the next four variables which represent the separators for your values which should be in subscript or superscript. E.g. H<sub>2</sub>O where “2” should be subscripted.  Please see farther below for an example input and output.

stringvar s; // set this to your field

// set these next four values to the separators used in your code

stringvar subS:= ‘<sub>’; // beginning of a subscript

stringvar subE:= ‘</sub>’; // end of a subscript

stringvar supS:= ‘<sup>’; // beginning of a superscript

stringvar supE:= ‘</sup>’; // end of a superscript

// the syntax below does not need to be changed

// this part deals with subscript

stringvar array sa:= split(s, subS);

while numbervar i < ubound(sa) do

(

i:= i + 1;

stringvar t:= sa[i];

if instr(t, subE) > 0 then

    (

    stringvar t1:= split(t,subE)[1];

    t1:= replace(t1, ‘0’, ‘₀’); // 0

    t1:= replace(t1, ‘1’, ‘₁’); // 1

    t1:= replace(t1, ‘2’, ‘₂’); // 2

    t1:= replace(t1, ‘3’, ‘₃’); // 3

    t1:= replace(t1, ‘4’, ‘₄’); // 4

    t1:= replace(t1, ‘5’, ‘₅’); // 5

    t1:= replace(t1, ‘6’, ‘₆’); // 6

    t1:= replace(t1, ‘7’, ‘₇’); // 7

    t1:= replace(t1, ‘8’, ‘₈’); // 8

    t1:= replace(t1, ‘9’, ‘₉’); // 9

    t1:= replace(t1, ‘+’, ‘₊’); // +

    t1:= replace(t1, ‘-‘, ‘₋’); // –

    t1:= replace(t1, ‘=’, ‘₌’); // =

    t1:= replace(t1, ‘(‘, ‘₍’); // left parenthesis

    t1:= replace(t1, ‘)’, ‘₎’); // right parenthesis

    t:= t1 + split(t,subE)[2];

    );

stringvar output:= output + t

);

// this part deals with superscript

s:= output;

i:= 0;

output:= ”;

stringvar array sa:= split(s, supS);

while numbervar i < ubound(sa) do

(

i:= i + 1;

stringvar t:= sa[i];

if instr(t, supE) > 0 then

    (

    stringvar t1:= split(t,supE)[1];

    t1:= replace(t1, ‘0’, ‘⁰’);

    t1:= replace(t1, ‘1’, ‘¹’);

    t1:= replace(t1, ‘2’, ‘²’);

    t1:= replace(t1, ‘3’, ‘³’);

    t1:= replace(t1, ‘4’, ‘⁴’);

    t1:= replace(t1, ‘5’, ‘⁵’);

    t1:= replace(t1, ‘6’, ‘⁶’);

    t1:= replace(t1, ‘7’, ‘⁷’);

    t1:= replace(t1, ‘8’, ‘⁸’);

    t1:= replace(t1, ‘9’, ‘⁹’);

    t1:= replace(t1, ‘+’, ‘⁺’);

    t:= t1 + split(t,supE)[2];

    );

stringvar output:= output + t

);

output

As a sample output, setting the variable s to the following

stringvar s:= ‘The human body is up to 75% H<sub>2</sub>O. O<sub>2</sub> accounts for about 20% of our air. Eight is 2<sup>3</sup>.’;

will produce the following on a report, using Arial Unicode MS font for the formula.

Untitled.jpg

If you do see some characters missing that you would like to add to this formula, please let me know. They would have to be characters that can be converted via a corresponding font character like the above character sets.

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Steve Elliott

    Very nice! This works for me. I have been asked to provide superscript versions for ordinals. I.e., 1st, 2nd, 3rd, 4th. That’s just the alpha portions.

    Steve

    (0) 
    1. Jamie Wiseman Post author

      thanks Steve. i haven’t found a free font that has letters in the subscript and superscript block. i created the above using Arial Unicode MS but it does not have those characters unfortunately. let me know if you can find a free font that does have those characters.

      -jamie

      (0) 
  2. Alexander Meadow

    Hi,

    I was wondering, for the superscript, how come you posted numbers 1-3 with an html tag of ‘&sub1;’ but for numbers 4-9 you used what looks like a smaller raised font.  At least, that’s what it looks like in the source code.  The reason I ask is when viewing on the screen in Crystal all the numbers work, but when printing to a PDF or a printer numbers 4-9 don’t render, while 1-3 do.  Thanks.

    (0) 
    1. Jamie Wiseman Post author

      hi Alexander and thanks for your comment. All of the subscript and superscript parts were copied and pasted directly from Windows > Character Map > Arial Unicode MS > Super/Subscript Unicode Range.

      I just double-checked and although I do see the same unusual looking spacing in the numbers greater than 3, the PDF export works fine for me. I use the test string of

      s:= ‘start sub <sub>0123456789+-=()</sub> endsub. start sup <sup>0123456789+</sup> end sup.’;

      I’m using version 14.1.5.1501 of CR.

      If you’re using a copy of CR that hasn’t been updated in a while perhaps try to update to see if that solves the export problem.

      (0) 
      1. Alexander Meadow

        I see what you mean.  When exporting from Crystal the superscript #4 is showing up, I usually print my reports from Business Objects InfoView, and from there I’m getting the box instead of the superscript 4.  Given that, do you think it’s likely and issue with IE printing a character that is not actually within the font (my font doesn’t have superscript 4, only 1,2,3).  I realize this may be outside youre expertise I’m just looking for any info on this I can get to try to get my head around my options.  Thanks again, for both the code and the replies.

        (0) 
        1. Jamie Wiseman Post author

          That would definitely be an issue regarding the font as this whole technique requires the proper font for the output. I’m not sure what OS you’re running your InfoView on but you may wish to use Arial Unicode MS as a test.

          (0) 

Leave a Reply