2

I want to do cross-references manually in microsoft word 2010 (similarly to latex \label - \ref or \cite - \bibliography). I found that the Field function does almost excactly what I want (the syntax is a bit weird). If I type the following to the document (wave brackets are field marks produced by ctrl+f9):

{set dischargeRate {seq Figure}}Figure {ref dischargeRate}: Discharge rate vs. hole diameter. Figure is from Reference [{ref authorA}].

The results are shown in Figure {ref dischargeRate} and published previously in [{ref authorB}] and [{ref authorA}].

References:

{set authorA {seq cites}}[{ref authorA}] author, title, journal, year

{set authorB {seq cites}}[{ref authorB}] author, title, journal, year

the above produces:

Figure 1: Discharge rate vs. hole diameter. Figure is from Reference [1].

The results are shown in Figure 1 and published previously in [2] and [1].

References:

[1] author, title, journal, year

[2] author, title, journal, year

Is there a way to define increment and reference with one command instead of those three commands: set, seq and ref? Or how do I create a macro that does this for me. I am looking something like {setOrRef sequencename labelname} that shows a number i+1 that can be later referenced by {setOrRef sequencename labelname}.

Also there should be a check that labels are not redefined. For example: If the label does not exist, the sequence (Figure or cites) is incremented by one and that number is assigned to the label. If the label exists the existing number for the label is used.

I consider this as a programming question as it so close to macros and automating Ms Word.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Juha
  • 2,053
  • 23
  • 44

1 Answers1

1

If you want to use field codes and nothing else, I don't think there is a practical way to avoid using SEQ, SET and REF.

There are a number of difficulties detecting the existence/non-existence of bookmarks. I think you can sidestep them using the following set of nested fields, but I cannot say I have tried this "for real". Personally, I would try to avoid this kind of complexity in field coding.

All you need to do is insert the bookmark name you want once in the QUOTE field, i.e. by substituting the "bookmark" name you want instead of "bm". Here, I use "s" as the name that provides sequential reference numbers. It isn't actually a bookmark name, by the way, but a SEQ name.

As usual, all the {} have to be the special field code braces that you can insert using ctrl-F9 on Windows Word. You will still need to be careful about your naming of references, e.g. don't use "AuthorA" and "AuthorA1". You will need to avoid using any other SEQ names such as AuthorA2, and avoid using SEQ names s and s1 elsewhere.

{QUOTE {SET b bm}{SEQ "{b}{SEQ {b}}" \r{SEQ "s{={SEQ {b}}-3 \#"1'';''"}}" \h}{SEQ "{b}1" \c}}

Since it's not obvious how this works, I'll step through. Suppose you name your reference "AuthorA". Then you would insert

{QUOTE {SET b AuthorA}{SEQ "{b}{SEQ {b}}" \r{SEQ "s{={SEQ {b}}-3 \#"1'';''"}} \h}{SEQ "{b}1" \c}}

Bookmark "b" is set to "AuthorA"

{SEQ "{b}{SEQ {b}}"} 

expands to

{SEQ "AuthorA{SEQ AuthorA}"}

In the first such set of fields for AuthorA , this expands to

{SEQ "AuthorA1" }

In subsequent sets, it expands to

{SEQ "AuthorA2" } 

and so on.

The final field (the one actually inserted by the QUOTE)

{SEQ "{b}1" \c}

expands to

{SEQ "AuthorA1" \c}

i.e. it is always the value returned by the first set of fields in the document for AuthorA.

The value for AuthorA1 is set by

\r{SEQ "s{={SEQ {b}}-3 \#"1'';''"}}

{SEQ {b}}

expands to

{SEQ AuthorA}

which will actually be 2 in the very first set of fields for AuthorA (this is one of Word's SEQ field evaluation quirks). So

{={SEQ AuthorA}-3}

will be -1 and the numeric switch

\#"1'';''"

will return ''. So AuthorA1 is set to

\r{SEQ "s"}

i.e. the next value of the sequence s.

Subsequently,

{={SEQ AuthorA}-3}

is greater than 0, the numeric switch returns '1' and so AuthorA2 will be set to

\r{SEQ "s1"}

But we don't care what AuthorA2 is set to. The purpose of this bit of numeric formatting is to ensure that { SEQ s } only increments the first time time we insert one of these sets of fields for a particular "reference name".

For inserting such stuff, you could create an autotext/building block that would insert the block. All you would need to do is change "bm". A piece of VBA to prompt for a bookmark name and insert the same block would not be hard, but I leave you to look around for that.

As always, even using the general technique suggested, there may be a way to simplify the fields. Personally, I think I would go for a design that let me insert some kind of placeholder and required me to run some "pre-publishing VBA" to set them up correctly.