Recent

Author Topic: Superscripts and subscripts  (Read 12014 times)

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Superscripts and subscripts
« on: June 23, 2017, 09:49:49 pm »
     I'm planning to use chemical formulas extensively in a project, so I'll need the ability to format text containing subscripts and superscripts.  I've installed RichMemo, and according to the wiki entry I should be able to write something like the following to format H3O+ so the '3' is subscripted and the '+' is a superscript:

Code: Pascal  [Select][+][-]
  1. RichMemo1.Text := 'H3O+';
  2. FontParams.vScriptPos := vpSubscript;
  3. RichMemo1.SetTextAttributes(1, 1, FontParams);
  4. FontParams.vScriptPos := vpSuperscript;
  5. RichMemo1.SetTextAttributes(3, 1, FontParams);

     But the compiler says VScriptPos, vpSubscript, and vpSuperscript do not exist.  Am I missing something?  Or are these parameters not actually implemented?  Is there an alternative to RichMemo I can use?  I've looked at TRichView too, but I can't afford it.  (Does it even work with Lazarus?  Or would I need Delphi?)  I've tried to install lzRichEdit, but I get a message that "lazrichedit does not have any 'Register' procedure".
     I'm using Lazarus version 1.6.4 and Windows 10.
Lazarus 1.8.0; fpc 3.0.4; Windows 10

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Superscripts and subscripts
« Reply #1 on: June 23, 2017, 10:01:38 pm »
Why don't you think of html? The TurboPower IpHTMLPanel comes along with the standard Lazarus installation and is used to display the codetools popup help windows. Look at the attached sample.
« Last Edit: June 23, 2017, 10:52:40 pm by wp »

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Superscripts and subscripts
« Reply #2 on: June 23, 2017, 10:40:23 pm »
I'll need the ability to format text containing subscripts and superscripts.
You might want to convert this Delphi component to Lazarus:
ChemTxt: Collection of components to display chemical formulae and equations
http://torry.net/vcl/science/packs/ChemText12.zip



ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Re: Superscripts and subscripts
« Reply #3 on: June 23, 2017, 11:27:43 pm »
     Thanks to both of you for your suggestions.  I've never used HTML before, but that little bit doesn't look too bad.  I'm not sure how to implement it into my project though.  I have several forms, each containing many TEdits representing various properties of some chemical - name, color, solubility, formula, etc.  The data for each chemical is combined and stored as a string that can be parsed to fill in the TEdits when the form loads.  Several years ago I wrote a Visual Basic program that would translate the strings representing the formulas into rtf that would be shown in rich text boxes (as I think they were called).  In addition, for entering new formulas, I had basically the same code in each rich text box's OnChange event handler to translate keyboard input into the appropriate rtf "on the fly".  (The code consisted essentially of finding each digit in the formula and making it a subscript or superscript depending on what preceded it in the formula.)  This worked very nicely and seemed to be efficient - I could store all the data for a substance as a single string instead of storing the formulas separately in rtf format.  I'm hoping to do the same in Pascal now.  Is it possible to write html code to be that flexible (remember I'm very new at this, so it should be simple), or should I stay with some kind of rtf control?
     As for ChemTxt, it looks interesting, but it will take me a while to understand the code.  I want the formula fields to look like TEdits.  Can ChemTxt do that?  And again, is it flexible enough to handle a formula without knowing what it is beforehand?
Lazarus 1.8.0; fpc 3.0.4; Windows 10

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Superscripts and subscripts
« Reply #4 on: June 23, 2017, 11:46:03 pm »
I got ChemTxt running on Lazarus now. But I don't know how to use it...

EDIT:
Now I found a copy of ChemTxt on the efg2 site (http://delphi.icm.edu.pl/ftp/d10free/chemtxt.zip) which contains a demo. So, you just write the formula - numbers are drawn as subscripts, + and - as superscripts. There are issues with reaction arrows, but they can be fixed.
« Last Edit: June 24, 2017, 12:46:14 am by wp »

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Superscripts and subscripts
« Reply #5 on: June 23, 2017, 11:55:21 pm »
Several years ago I wrote a Visual Basic program that would translate the strings representing the formulas into rtf that would be shown in rich text boxes
What does a string representing a formula look like? Maybe I can adapt ChemTxt to follow your syntax.

For the HTML case, you just write <sub> to begin a subscript and </sub> to end it. Likewise you use <sup> and </sup> to enclose a superscript. Very easy. So, to display H2O you write H<sub>2</sub>O. And to get a valid html string you enclose the entire string to be displayed by '<htm><body>' and '</body></html>'.

Example: If you want to display "The chemical formula of water is H2O" you write
Code: [Select]
<html><body>The chemical formula of water is H<sub>2</sub>O</body></html>

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Superscripts and subscripts
« Reply #6 on: June 24, 2017, 02:13:54 am »
I've installed RichMemo, and according to the wiki entry I should be able to write something like the following to format H3O+ so the '3' is subscripted and the '+' is a superscript.
     But the compiler says VScriptPos, vpSubscript, and vpSuperscript do not exist.  Am I missing something? 

What version did you install? Try the version from this link

the following rich memo code works for me.

Code: Pascal  [Select][+][-]
  1. var
  2.   FontParams: TFontParams;
  3. begin
  4.   RichMemo1.Text:='H3O+';
  5.   RichMemo1.GetTextAttributes(1,FontParams);
  6.   FontParams.vScriptPos := vpSubscript;
  7.   RichMemo1.SetTextAttributes(1, 1, FontParams);
  8.   RichMemo1.GetTextAttributes(3,FontParams);
  9.   FontParams.vScriptPos := vpSuperscript;
  10.   RichMemo1.SetTextAttributes(3, 1, FontParams);
  11. end;      
  12.  

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Re: Superscripts and subscripts
« Reply #7 on: June 24, 2017, 04:14:38 pm »
OK.  I reinstalled RichMemo - and it works!  I don't pretend to understand.  Thank you all for your help and suggestions.  For those few who might be wondering, I don't think HTML would have worked because I'm not writing a document where each formula is in a specific known spot.  I have more of a fill-in-the-blank data entry situation where formulas can be entered and edited in real time.  RichMemo, or something like it, is ideal for what I'm doing.  Again, thanks to all.
Lazarus 1.8.0; fpc 3.0.4; Windows 10

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Superscripts and subscripts
« Reply #8 on: June 25, 2017, 06:45:53 pm »
I reworked the chemtxt library and uploaded the Lazarus version to ccr: https://sourceforge.net/projects/lazarus-ccr/files/chemtext/chemtext-0.1.zip/download.

The component TChemLabel will install into the Misc component palette. It is an (almost) full-fledged label now and supports all TLabel properties such as Alignment or Layout (but not WordWrap, BiDiMode, ShowAccellChar). Reaction arrows can be drawn with "ASCII art" as "->" or using the corresponding UTF8 character (use property Arrow).

[EDIT]
There is now also a wiki page: http://wiki.lazarus.freepascal.org/ChemText
« Last Edit: June 25, 2017, 11:42:55 pm by wp »

William Marshall

  • Jr. Member
  • **
  • Posts: 52
Re: Superscripts and subscripts
« Reply #9 on: June 25, 2017, 11:37:30 pm »
Thanks, wp.  I downloaded ChemText, and it looks interesting.  Right now, though, I need something interactive, like a TEdit that can handle rtf.  I've been playing around with RichMemo with some, if not complete, success.  But I may very well use ChemText later in my project.  Thanks again.
Lazarus 1.8.0; fpc 3.0.4; Windows 10

djzepi

  • New Member
  • *
  • Posts: 36
Re: Superscripts and subscripts
« Reply #10 on: June 26, 2017, 12:02:11 pm »
You could use UTF-8 char
- http://wiki.freepascal.org/UTF-8_Latin_characters
- http://wiki.freepascal.org/UTF-8_subscripts_and_superscripts

e.g.
Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.FormCreate(Sender: TObject);
  3. const
  4.   SUB_2 = #$E2#$82#$82;
  5.   SUB_3 = #$E2#$82#$83;
  6.   SUP_PLUS =  #$E2#$81#$BA;
  7. var
  8.   Memo1:TMemo;
  9.  
  10. begin
  11.   Memo1:=TMemo.Create(Self);
  12.   Memo1.Parent:=Self;
  13.   Memo1.Align:=alClient;
  14.   Memo1.Lines.Clear;
  15.   Memo1.Lines.Add('Ion-molecule reaction: '+'H'+SUB_2+SUP_PLUS + ' + '+
  16.   'H'+SUB_2 +'     ' + 'H'+SUB_3+SUP_PLUS + ' + '+'H' );
  17. end;
  18.  
  19.  

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Superscripts and subscripts
« Reply #11 on: June 26, 2017, 12:37:10 pm »
Good idea.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Superscripts and subscripts
« Reply #12 on: June 26, 2017, 12:50:43 pm »
The trouble with the Unicode approach is the restricted field of characters available.
Can you represent nonane (C9H20) using this approach?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Superscripts and subscripts
« Reply #13 on: June 26, 2017, 01:57:21 pm »
superscript 2 and 3 are already part of unicode  as $00B2 and $00B3

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: Superscripts and subscripts
« Reply #14 on: June 26, 2017, 02:21:20 pm »
Can you represent nonane (C9H20) using this approach?
Certainly:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. const
  3.   SUB_9 = #$E2#$82#$89;
  4.   SUB_2 = #$E2#$82#$82;
  5.   SUB_0 = #$E2#$82#$80;
  6. begin
  7.   Memo1.Lines.Clear;
  8.   Memo1.Lines.Add('Nonane (C9H20): ' + 'C' + SUB_9 + 'H' + SUB_2 + SUB_0);
  9. end;
In my feeling these sub/superscripts are a bit too small, however. But the main point is that this approach works only if these codepoints are available in the current font.

 

TinyPortal © 2005-2018