Fraction Macro

Last week's newsletter explained how to make your own typographical fractions in Microsoft Word. You can read about the technique here:

http://www.topica.com/lists/editorium/read/message.html?mid=1709983687

But if your document is full of plain-text fractions, like these--

1/3 2/3 5/8

--why not let a macro do the work? I owe my thanks to Wordmeister Steve Hudson for the idea. Steve would probably take a more elegant approach, but this macro will definitely work. Enjoy! If you don't know how to use macros like this one, you can learn how here.

And now, the macro:

'THE MACRO STARTS HERE
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "([0-9]@)/([0-9]@)"
.Replacement.Text = "|sp|1|sp|" + ChrW(8260) + "|sb|2|sb|"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Superscript = True
.Subscript = False
End With
With Selection.Find
.Text = "|sp|([0-9]@)|sp|"
.Replacement.Text = "1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Superscript = False
.Subscript = True
End With
With Selection.Find
.Text = "|sb|([0-9]@)|sb|"
.Replacement.Text = "1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
'THE MACRO ENDS HERE

What's basically going on here is that Word uses a wildcard search to find all numbers that have a slash between them:

Find What: ([0-9]@)/([0-9]@)

Then it replaces those numbers with *themselves* surrounded by arbitrary codes denoting superscript (|sp|) and subscript (|sb|). It also replaces the slashes with the fraction bar character, Unicode number 8260:

Replace With: "|sp|1|sp|" + ChrW(8260) + "|sb|2|sb|"

(If you were doing this by hand, you could insert the fraction bar into your document using Insert > Symbol [as explained in last week's newsletter], then copying it, and then pasting it into the Replace dialog's "Replace With" box. The result would look something like this: |sp|1|sp|/|sb|2|sb|)

Finally, the macro uses parentheses to group the numbers so that after they and their surrounding codes are found, the numbers can be replaced by *themselves,* properly formatted and without the codes, using the "Find What Expression" wildcard:

Find What: |sp|([0-9]@)|sp|

Replace With: 1 [formatted as superscript]

Find What: |sb|([0-9]@)|sb|

Replace With: 1 [formatted as subscript]

You can learn more about wildcard searches here:

http://www.topica.com/lists/editorium/read/message.html?mid=1705963026

http://www.topica.com/lists/editorium/read/message.html?mid=1706069286

http://www.topica.com/lists/editorium/read/message.html?mid=1706167662

http://www.topica.com/lists/editorium/read/message.html?mid=1706267069

http://www.topica.com/lists/editorium/read/message.html?mid=1706365638

http://www.topica.com/lists/editorium/read/message.html?mid=1706458823

_________________________________________

READERS WRITE

Gentle reader, I've run into a fairly serious problem that I'm hoping you may have some ideas about. The problem is that QuarkXPress doesn't import Unicode characters--the old 256 are all it will deal with. Several people have asked me if there's a way to find and replace Unicode characters in Word with other lower-level characters that Quark could deal with. For example, if you type a, b, c in Quark and then format it with a Greek font, you get alpha, beta, gamma. Maybe a similar approach could be taken with Unicode characters, if you could figure out what to replace them with in Word (and actually do the replacing) before importing the Word document into Quark. I don't know--this looks like an almost insurmountable problem to me. If you are successfully importing Unicode characters into QuarkXPress, especially using XPress Tag files, I'd love to hear from you. Please write to me here: mailto:editor [at symbol] editorium.com

Sage Rountree (srountree@dukeupress.edu) wrote:

"When we insert notes to compositor and authors in electronic manuscript files, we want those inserts to be bold and in either angle brackets (for coding and notes to compositor) or curly brackets (for queries to authors). Sometimes, our freelance copyeditors neglect to toggle on the bold for these queries, and we go through and manually convert them to boldface. (By manually, I mean we search for the opening bracket, highlight, and toggle--a big waste of time.)

"I'm able to figure the search-and-replace for toggling short coding with fixed letters (, ) to boldface, but how can I replace all text between the brackets <> and {} *with the brackets themselves* and the text they contain in bold? The substance of the note can sometimes be a few sentences long, and I don't know how to denote that with wildcards. It's like the opposite of the process you outlined in the 10/25/2000 newsletter on replacing with "find what text."

"This has been an interesting mental puzzle for me, but I'm ready to throw in the towel.

Minutes later, before I could respond, Sage wrote again:

"Jack, I spoke too soon about throwing in the towel, and I added the backslash so Word would recognize the angle brackets as characters, not as the start and end of words. To that end, I wrote this macro:

Selection.HomeKey Unit:=wdStory

Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting

Selection.Find.Replacement.Font.Bold = True

With Selection.Find

.Text = "<*>"

.Replacement.Text = "^&"

.Forward = True

.Wrap = wdFindContinue

.Format = True

.MatchCase = False

.MatchWholeWord = False

.MatchWildcards = True

.MatchSoundsLike = False

.MatchAllWordForms = False

End With

Selection.Find.Execute Replace:=wdReplaceAll

Many thanks to Sage for this useful macro (which I edited slightly). I'd add that if your comments are between curly brackets, you could replace the sixth line with this:

.Text = "{*}"

_________________________________________

RESOURCES

So, you kind of like all this programming stuff, eh? Want to learn more? There's an excellent tutorial, "Getting to Grips with VBA Basics in 15 Minutes," by Word expert Bill Coan, at the MVPS Web site. You'll find the tutorial here:

http://www.mvps.org/word/FAQs/MacrosVBA/VBABasicsIn15Mins.htm

This entry was posted in Typesetting. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

You must be logged in to post a comment.

  • The Fine Print

    Thanks for reading Editorium Update (ISSN 1534-1283), published by:

    The EDITORIUM, LLC
    http://www.editorium.com

    Articles © on date of publication by the Editorium. All rights reserved. Editorium Update and Editorium are trademarks of the Editorium.

    You may forward copies of Editorium Update to others (but not charge for it) and print or store it for your personal use. Any other broadcast, publication, retransmission, copying, or storage, without written permission from the Editorium, is strictly prohibited. If you’re interested in reprinting one of our articles, please send an email message to editor@editorium.com

    Editorium Update is provided for informational purposes only and without a warranty of any kind, either express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, and freedom from infringement. The user (you) assumes the entire risk as to the accuracy and use of this document.

    The Editorium is not affiliated with Microsoft Corporation or any other entity.

    We do not sell, rent, or give our subscriber list to anyone. Period.

    If you’d like to subscribe, please enter your name and email address below. We publish the newsletter once a week, and on rare occasions we may send an important announcement. We never, ever send spam. Thank you for signing up!