Scientific Macro

Get help using and writing Nisus Writer Pro macros.
Post Reply
patrickg
Posts: 5
Joined: 2004-09-13 05:41:49

Scientific Macro

Post by patrickg »

Used NW most of my teaching career. I was a chemistry teacher and you can imagine the formatting problems that came up with subscripts, superscripts, and powers. Several years ago I created a NW macro (I call it "Scientific") to handle all of these to once and would like to share it with anyone who might find it useful. The code is below. Just copy it paste to a new document and add it to the NWP macro menu as described in the manual. The nice thing about this macro is that you can type up the entire document, highlight all of the text, and everything is taken care of with one macro. Use the following rules when typing:
Enter Formulas as: Ca3(PO4)2 or Fe4[(SCN)6]3
Enter Charges to be raised to powers, like for ions, as: H3O1+ or SO42-
Enter Powers of ten as: 6.02 X 1023 or 1.66 X 10-24
Highlight all of the text and select the macro.

Enjoy.

#!/usr/bin/perl
#Nisus Macro Block
#source front selection
#destination front selection
#Send Text as RTF
#End Nisus Macro Block

@myfile=<STDIN>;
foreach $s (@myfile){
$s=~s/([A-Z][a-z]?[0-9]*)([0-9][+-])/$1\{\\super $2\}/g;
$s=~s/([A-Z][a-z]?)([0-9]*[+-])/$1\{\\super $2\}/g;
$s=~s/\)([+-])/\)\{\\super $1\}/g;
$s=~s/\)([0-9]+)([0-9][+-])/\)\{\\sub $1\}{\\super $2\}/g;
$s=~s/([A-Z][a-z]?)([0-9]+)/$1\{\\sub $2\}/g;
$s=~s/\)([0-9]+)/\)\{\\sub $1\}/g;
$s=~s/\]([0-9]+)/\]\{\\sub $1\}/g;
$s=~s/(\sX\s10|\sx\s10)([+-]?[0-9]*)/$1\{\\super $2\}/g;
print $s;
}

# end of script
NisusUser
Posts: 317
Joined: 2011-01-12 05:32:38

Re: Scientific Macro

Post by NisusUser »

Hi, Patrick G,

I tried your macro. Thank you! Here's what I got:
Scientific Notation Sample.png
Scientific Notation Sample.png (19.26 KiB) Viewed 2381 times
Because of your background teaching chemistry, I'll ask you (or others who may know): Do you know of a way in Nisus Writer Pro to do fractions such that the numerator is above a line and the demoninator below (i.e., not a slanted line fracton).

WORD has Scientific Notion (Equations) that does that, but I don't know how to do it in Nisus, which I prefer to use. It may not be possible. In some cases, I actually need to be able to present the denominator in a smaller font than the numerator – or vice versa.

And I don't want to use text boxes. They are very inconvenient.

The subscription-only, overpriced page layout software InDesign can be expensively tweaked with a plug-in (also subscription) from www.indesignmathtools.com, but I've not found any inexpensive way to do those type of fractions other than in WORD.

Thanks for the macro!
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Scientific Macro

Post by adryan »

G'day, patrickg et al

Thanks for that. A nice addition to the Macro collection.

A couple of things others may need to take note of:–

(1) The last line of the instructions should read: "Select any text containing your expression(s) and run the macro." (Highlighting is something different. And the selection can include other stuff as well.)
(2) To run from a .nwm document, you need to preface the code with "Begin Perl" and conclude it with "End".

On the subject of fractions, I think Nisus Writer used to have access to an Equation Editor. Maybe it was MathMagic Lite (which is free), but macOS baulks at opening the latest version, seemingly because of security issues with a non-Apple-approved product. Unless it's absolutely necessary, I'm reluctant to buy single-purpose software when I can find ways to use what I already have.

It is possible to create a fraction in GraphicConverter (which is not free, but which many serious Mac users probably have anyway), using the text and line tools. Then put a rectangular selection box around your expression, create a new image from the selection, and save it somewhere convenient. (Tip: If you're creating a number of such images in a single session, use Save Copy As rather than Save because the latter will flatten text and make subsequent editing impossible.) Insert your image inline with text in a NWP document. Judicious alteration of Shape Padding and Metrics can be used to obtain a pleasing result. Complicated formulae (such as integrals and limits) can be achieved in this way as well. Creating the expression as a graphic image has the advantage that symbol size and position are easily manipulated. With a bit of experimentation you should be able to fix on parameter values that suit your purposes. So that's what I would do, but I'd be interested to see others' suggestions.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
NisusUser
Posts: 317
Joined: 2011-01-12 05:32:38

Re: Scientific Macro

Post by NisusUser »

NisusUser wrote: 2023-01-28 18:51:20 […] I've not found any inexpensive way to do those type of fractions other than in WORD.
For more completeness, let me mention something I had forgotten: when I was looking for a good solution for publishing fractions, I found that LibreOffice does Equations also, but I found it less pleasing to the eye than what WORD does. (I assume other flavors of open source word processors that are based on the same base code as LibreOffice, e.g., NeoOffice, also do fractions, but I didn't research them after trying LibreOffice's solution.)

@adryan, thanks for chiming in. What I am doing requires that the numerator and denominator not be turned into images, so the Graphic Converter approach is not a good one for me. I have GC and use it nearly every day for little things like reducing the size of screenshots, etc.
patrickg
Posts: 5
Joined: 2004-09-13 05:41:49

Re: Scientific Macro

Post by patrickg »

Hi Adrian and Others,
Thank you for correcting my usage of "Select Text" and "Highlighting". I am having problems with "Selected Text" and the way it displays on my screen (its very hard to see the light highlighting), and I get the two confused. I'm glad the macro is working properly for you. I know it saved me a lot of time formatting documents containing subscripts, superscripts, and powers of 10 while teaching.

Regarding the problem of having NWP display fractions properly, that is without the slant, I cannot offer much help. I use two other programs to help with complex math and scientific expressions: MathMagic Lite, and Open Office. Both are free. Open Office contains a formula module that will allow you to create some math symbols, the fraction you are looking for is available in this module. MathMagic contains many more features. What I have done in the past is create the formula or expression in one of these applications and then do a screen shot and paste it into my document. The Mac commands to select and save a portion of the screen to the clipboard is: "Command-Control-Shift 4". After issuing this command you can select a portion of the screen and have it saved to the clipboard where it can be pasted into a document. It would be better if NWP had something similar to this.
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Scientific Macro

Post by adryan »

G'day, NisusUser et al

Undeterred by rejection of my previous suggestion, I offer a couple more.

The first is that we really require a fraction with a horizontal line separating numerator and denominator to appear as a glyph. So there may be some inexpensive font creation software out there that would do the trick.

My second suggestion tries to get around the central problem which is that, although you can shift the baseline to adjust the vertical placement of the numerator and the denominator, you can't shift the horizontal placement to position one on top of the other. (You could use one of the combining double macron glyphs from the Unicode set to serve as the horizontal bar.) But it might be possible to use the Watermark feature to act as a background layer on which to position a denominator (say). A cunningly contrived Macro might then adjust position on the background layer to maintain the desired alignment when text on the main front layer is altered (as with word insertions, for example). Numerator and denominator could still be treated as numbers for mathematical manipulations if required.

Not convinced?

How about using a simple "÷" sign instead? Or a negative exponent? Or perhaps something with a bit more literary flair, like "antilog (log N – log D)"?

I suspect anyone fussy enough not to opt for one of these truly fabulous solutions will also be too fussy to spring for a fully font-compatible decimal expansion instead. Sigh!

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
Vanceone
Posts: 211
Joined: 2013-05-03 07:06:31

Re: Scientific Macro

Post by Vanceone »

The best mathematical typesetting, by far, is via LaTex. What I find works is to install a LaTex suite, (MacTex is preferred) and then the program "LaTexIt" which is designed to allow you to do math including fractions and then insert them into other apps.

I just tried it out again. It does insert an image of the fraction into Nisus Writer... but it's inline, and you can double-click it to have it open in LaTexIt and edit the equation. It then updates into Nisus automatically. So much like the old "equation editor" software. And LaTex and LaTexIt is 100% free.

You can also look at the "insert" menu, the "Special Characters" submenu, and then the "Math" section, which allows you to insert some very common fractions like 1/3 except they are typographically much more compact, and are one character.
NisusUser
Posts: 317
Joined: 2011-01-12 05:32:38

Re: Scientific Macro

Post by NisusUser »

@ adryan & Vanceone,

Thank you very much! I'll see what works best. I appreciate the help.
Post Reply