Word underline

Everything related to our flagship word processor.
Post Reply
Yohanan
Posts: 85
Joined: 2017-12-21 04:36:37

Word underline

Post by Yohanan » 2018-12-23 09:20:56

Hi,
Am I correct in assuming that there is not in Nisus a Word underline, though the new line is much fetter Thant it was in the precedent version.

adryan
Posts: 242
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Word underline

Post by adryan » 2018-12-23 17:02:07

G’day, Yohanan et al

I think you are right, Yohanan. There appears to be no such command. But it is easy enough to create a simple macro that does the job for you.

However, in my opinion anyway, one needs to be a bit more subtle than one might at first think. I would like all hyphenated words to have the enclosed hyphen(s) underlined as well. Also, standalone numbers (which may include commas, spaces or periods, according to various formatting schemes) should have the full number underlined, as well as any leading or trailing currency symbols. And it would be nice, too, to have some numeric-prefixed expressions (such as “2-dimensional” and “2,4,6-hydroxywhatever”) completely underlined.

They would be my default preferences. In some situations, such as in cross-referencing or hyperlinking, one might not wish to have a numeric prefix underlined, but it is easy enough to remove it in such cases. For most purposes, I think it’s better to include the lot in the initial underlining process.

With a bit of fooling around, I managed to get a macro that does what I want — mostly. In the process, though, I discovered a curious thing: the “|” operator in the Find/Replace system is not commutative.

Attached is a file for consideration. It begins with two versions of a macro; the arguments in the Find expression in the first are swapped to produce the second. Then follows a “Lorem ipsum” chunk of text in which I have inserted examples of the expressions alluded to above. Text to be selected for the purposes of testing the macro is colored red.

One of the macros fails to underline hyphens in numeric-prefixed expressions, while the other fails to underline commas, spaces or periods in numbers.

Comments, anyone?

Cheers,
Adrian
Attachments
Lorem ipsum.rtf
(21.93 KiB) Downloaded 34 times
MacBook Pro (mid-2014)
macOS Mojave 10.14.3
Nisus Writer user since 1996

User avatar
phspaelti
Posts: 989
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Word underline

Post by phspaelti » 2018-12-23 21:41:42

adryan wrote:
2018-12-23 17:02:07
With a bit of fooling around, I managed to get a macro that does what I want — mostly. In the process, though, I discovered a curious thing: the “|” operator in the Find/Replace system is not commutative.
If you had asked me beforehand I wouldn't have been able to answer that, but I'm not surprised. Regex is at its base an algorithmic specification. So it is going to read from left to right. And it is greedy, so if the beginning can match something it will, and (as linguists say) "bleed" the latter part of the expression.
Your problem ultimately results from beginning your expression with optional stuff (like \p{Sc}?). You should always try to start your expression(s) with required elements. One good idea is to use delimiters ((like "beginning of word"). Unfortunately for you, the currency symbols that you want to include are not included in the word characters (though the number-digits are). Also you might want to use character sets to make things more readable. Note that you can write things like [[:digit:],.] or [\d,.] to find all digits as well as commas and periods.

With that said, most people would probably be satisfied with the following:

Code: Select all

Find '\<[\w-]+\>', 'Ea'
which will pick up all words, including words with hyphens and numbers. The delimiters ensure that you don't get "words" beginning or ending in a hyphen.

One (unsurmountable?) problem is things like "2-dimensional": If you look for numbers first, the "2" will be found; if you look for words first, you will end up selecting the digit clusters in numbers like "1,000" as words rather than digits. To get around this problem you could try using "Followed By" delimiters, i.e., look for numbers first, but match only numbers not followed by hyphen.

Personally I would recommend doing this with more than one expression. You can always apply underline twice. :wink:
philip

adryan
Posts: 242
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Word underline

Post by adryan » 2018-12-23 22:46:59

G'day, Philip et al

Thanks for that, Philip.

It’s very naughty of regex to treat “|” in this way. It’s somewhat akin to an “exclusive or”, I guess.

A few observations: I’m not so sure that currency symbols are the problem here, as the expressions I gave find and treat them as expected. We don’t want to find and underline all commas, periods and spaces — only those that form part of a number. And we need to be careful about hyphens in connection with numbers because some people use hyphens as negation or subtraction signs (when in my opinion they should be using en dashes, but that’s another story).

Having said all that, I see the problem with my approach as being related to the “|” operator in this context. The fact that each of the compound expressions I gave almost does the job, but in a different way, supports this.

I agree with your recommendation of using more than one expression: it’s probably the best way to go. In fact, I considered this, but didn’t proceed down this route for two reasons: first, I thought it was best to clarify the situation with the “|” operator, and second, I confess I do not really know how to confine the operation of a second Find/Replace expression to the initially selected text when the latter has been altered by the first Find/Replace expression. Actually, now I come to think of it, one could place the original selection in a Clipboard, perform all the desired machinations on it there, and then paste the result back into the original document. But you may have an alternative method, Philip.

When all is said and done, and in response to Yohanan’s original query, I think it would be a good idea to include such a macro in the macro suite supplied with Nisus Writer. Even better, given that I suspect many people would find it useful from time to time, this style of underlining could be incorporated into the options available in the Underline section of the Format menu.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.3
Nisus Writer user since 1996

User avatar
phspaelti
Posts: 989
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Word underline

Post by phspaelti » 2018-12-23 23:34:26

Well, all of this quickly runs into the usual problem: should I write a quick-and-dirty macro that works for me, or try to create a new feature that works in the general case. I tend to find the latter to be something with diminishing returns. I personally believe one should avoid underline altogether, never mind word-underline. My documents also tend not to include numbers with currency symbols, or chemical formulas, so I don't have a real opinion on what one should do in such cases.

Returning to the (macro) problem of combining multiple search expressions to create a single selection, there are 3 approaches:
  1. Use "Replace in Selection" with an "S" option to keep the selection between searches
  2. As you suggest, work on the clipboard and paste the result
  3. Use the macro language. This has the problem that there is no direct way to do "Find in Selection"
Here is a macro that uses approach 3. I'll let you improve it to your liking. :wink:

Code: Select all

# Get the current selection
$sel = TextSelection.active
$txt = $sel.substring

# Find bits
$bits = $txt.find '\<[\w+-]+\>', 'Ea'
$bits.appendValuesFromArray $txt.find '\p{Sc}?\d(?:[\d,. ]*\d)*\p{Sc}?', 'Ea'

# Create selections from found bits
$sels = Array.new
foreach $bit in $bits
    $sels.push TextSelection.new($sel.text, Range.new($sel.location + $bit.location, $bit.length))
end

# Apply Underline
Push Target Selection $sels
Set Underline 1
Pop Target Selection
philip

adryan
Posts: 242
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Word underline

Post by adryan » 2018-12-24 00:54:19

G'day, Philip et al

I agree with you, Philip. I myself rarely use underlining and never use "word underlining" in Yohanan's sense. But the question was posed and I thought I'd have a little play.

And the choice between a "quick-and-dirty" specific-situation solution and a solution applicable in the more general case is one I frequently face as well. The mathematician in me favors the latter, but the exigencies of life usually favor the former. As a consequence, I will often use "quick-and-dirty" Find/Replace methods rather than delve into the intricacies of the macro language which I have never had time to study in detail and hence use in a most eclectic way. (You would be horrified, Philip!) But every now and then I will put a lot of effort into devising a macro or an AppleScript script capable of general utility in some context which I anticipate will arise frequently for me.

I'm keen to try out the macro you posted, as well as consider the other options you mentioned. But it will have to wait a bit, as I've spent too much time in front of the screen for one day!

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.3
Nisus Writer user since 1996

Yohanan
Posts: 85
Joined: 2017-12-21 04:36:37

Re: Word underline

Post by Yohanan » 2018-12-24 05:20:15

Thank you all for the answers. They are a bit complex and the suggestion by Adrian to add this kind of underlining in the menu sounds good to the novice I am. I totally agree with the fact that it is neither elegant nor very useful to have this kind of underlining in a published text. Fact is that I use this mainly in my great number of papers for communications and lengthy conferences, in which I resort to diverse signs, arrows and styles. It helps me to talk to the audience without clinging too much to my rather detailed notes. They are kind of diacritic signs to catch with a glimpse the kind of remark I am going to give. :)
Happy Christmas.

adryan
Posts: 242
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Word underline

Post by adryan » 2018-12-24 21:35:33

G’day, Philip et al

Your macro works brilliantly, Philip! I deliberately included a few tricky situations in that “Lorem ipsum” text, and your macro negotiated them perfectly. So thanks for that.

All you need to do, Yohanan, is:–

(1) Copy Philip’s macro code
(2) Macro > New Macro
(3) Paste
(4) Save the document in an appropriate place in the Macros folder with an appropriate name (eg, “Underline Words in Selection”)

Then, in any NWP document, select the text you want underlined in this fashion, go to the Macro menu, navigate to the Underline Words in Selection macro and select it.

Done!

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.3
Nisus Writer user since 1996

Yohanan
Posts: 85
Joined: 2017-12-21 04:36:37

Re: Word underline

Post by Yohanan » 2018-12-25 04:46:05

Thank you, it works nicely.

Yohanan
Posts: 85
Joined: 2017-12-21 04:36:37

Re: Word underline

Post by Yohanan » 2019-01-12 02:54:05

Thank you for this, but now I wonder: is the language of the macros a matter of specialists ? Or is there a place in the manual where to work out a macro is explained ? For example, if I want to change a font to another throughout a document. I tried Find and replace but (I am not surprised) this does not work.
Thanks in advance.

Thanks a lot to Martin who solved my root problem with the Doc Manager,

Yohanan
Posts: 85
Joined: 2017-12-21 04:36:37

I did one!

Post by Yohanan » 2019-01-12 03:02:12

Following the Manual page 415 I could simply apply to the whole document the Times New Roman font. It worked!

Yohanan
Posts: 85
Joined: 2017-12-21 04:36:37

Word underline again

Post by Yohanan » 2019-01-15 00:37:02

For some reason I cannot explain, the macro edited by Phillip does not work anymore. All the more unfortunate for it produced a fine line and not th thick line offered at first by Nisus for any underlining. This may be related to the fact that with the help of Martin I solved de question of Doc Managers, things being now in their right place in ma Nisus Documents folder.

Strangely enough too, the fact that some quick fix entries through with I had kind of a shortcut does not work as fine as before. For example, I had one with a superscript bold letter at the end of the word and now this letter is no more superscript nor bold. I tried to have it through the Glossary, but this does not work at all.

adryan
Posts: 242
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Word underline

Post by adryan » 2019-01-15 01:58:41

G’day, Yohanan et al

First, quit and relaunch Nisus Writer Pro. In fact, although it shouldn’t be necessary, why not restart the entire computer and then launch NWP, just to be sure.

Next, select Philip’s macro from the hierarchical folder system in Nisus Writer’s Macro menu and see whether it works. Don’t forget that it operates on the frontmost (open) Nisus document and expects to find some text selected in it.

If the macro still does not work, select some other macro command from the same folder and see whether it works. If this second macro doesn’t work, either you aren’t supplying the macro with its requirements (eg, selected text), or the Macros folder is not where it should be, or something is corrupted and you should consider reinstalling NWP.

If the second macro works as expected, locate it in the Finder and give it a distinctive name (eg, append “XXXX” to the filename). Now ensure that this macro file is the one that appears in the Nisus Macro menu. If it doesn’t, you’re somehow working with the wrong Macros folder.

If you’re happy you’re working with the correct Macros folder and the second macro you chose in it works OK but Philip’s does not, trash his macro (sorry, Philip!), then reinstall it from the above code (there, I’ve redeemed myself already!), making sure that it goes in the same folder that contained the second macro that worked. Then restart the computer (again, just to be really sure) and launch NWP.

If Philip’s macro still does not work, I’m afraid I’ve run out of ideas.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.3
Nisus Writer user since 1996

User avatar
martin
Official Nisus Person
Posts: 4462
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Word underline again

Post by martin » 2019-01-15 07:36:42

Yohanan wrote:
2019-01-15 00:37:02
All the more unfortunate for it produced a fine line and not th thick line offered at first by Nisus for any underlining
How thick a line is shown for any particular text underline is controlled by the font. The macro and Nisus Writer itself have no control over the underline thickness. If you want a thicker underline you will have to try different fonts.
some quick fix entries through with I had kind of a shortcut does not work as fine as before. For example, I had one with a superscript bold letter at the end of the word and now this letter is no more superscript nor bold
QuickFix typo entries (under "Fix typos" in your preferences) cannot enforce formatting like bold or superscript. Those entries are designed just to correct minor typos (change "teh" into "the") and other kinds of short textual changes. They do not apply formatting and never have.
I tried to have it through the Glossary, but this does not work at all
Glossary expansions (under "Expand entries from glossary files as you type") can indeed enforce formatting. This is the correct way for an abbreviation to expand into formatted text or other rich content.

If your glossaries aren't working, are you sure they are enabled? Are the corresponding glossary files checkmarked in your QuickFix preferences? In the following screenshot you can see my "colors" glossary is enabled:
prefs.png
prefs.png (22.59 KiB) Viewed 1233 times

Post Reply