Setting and using LibreOffice Writer fields, variables, and formulae

I found this interface really difficult to use; hopefully some of the following step-by-step examples will assist someone else struggling with it.

Creating one field (or more) containing a user-set variable

  1. Open a new LibreOffice Writer document
  2. The Fields interface can be opened with either:
  3. Let's create a variable i
  4. Ensure Set variable is selected in the Type column
  5. Type i in the Name input field, and put a date value in the Value input field
  6. Click on Additional formats... in the Format column (don't choose General as highlighted in blue in the screenshot)
  7. Select the Category of Date (yes; it is usually necessary to click on Date twice to select it here) and the preferred date Format, e.g. DD/MM/YYYY, then click [OK]
  8. Click [Insert] to place the field containing the variable into the document
  9. Double-clicking on the field (anywhere in the grey) will allow one to edit the value, although note that the formatted date has been converted to an integer
  10. Click [Cancel] (or the closing X on Edit Fields), then Ctrl-F2 to re-open Fields
  11. Note that i now appears; if one changes the Type to Show variable, and position the insertion point in the document away from the first-inserted field, double-clicking on the i under Select will add another field with the same value into the document. Changing the first-inserted field's value will change the other/s immediately.
  12. Good luck trying to figure out how to remove a variable from this screen, however. The objective here is to make the disabled red cross become enabled, changing from to
  13. Just to eke out the drama, one cannot select i and hit [Delete]; one cannot type i into the Name input field and enable the red Delete button...
  14. The only way (and it does make sense, but there are no visual clues to indicate this) to delete that variable is to remove all instances of the fields containing that variable from the document - the Fields interface can stay open while one does this. (The exception to this is removing a variable with an empty value - the field can still remain on the document. This is not a particularly useful scenario...)
  15. After deleting every field containing the variable out of the document, manually insert i into the Name input field. Magically the red Delete button will become enabled.

Creating a formula referring to a user-set variable

Feel free to use the same document as before.

  1. Following a similar process to that starting in Step 4 above, create a new variable j and insert it into the document
  2. Move the insertion point in the document to another location close by
  3. Change the Type to Insert Formula
  4. Double-click on j to insert it into the Formula input field
  5. Append `+ 1' to the j
  6. Click [Insert] to place the results of adding 7 to j into the document (note the 694 and 701 on the left of the Fields interface):
  7. Once again, one can double-click on the field to edit the variable/formula:

Various questions and answers which may be useful

Q1: What is the difference between an instance of a variable and a User Field?

A1: As an example, a variable i could be inserted on page 1, then inserted again on page 2. If redefined in the Fields interface (even with the same variable name i), it could be inserted on page 3. As long as each instance of i is created independently, each one can be edited individually (which kinda removes the point of variables...). A User Field will always have exactly the same value no matter how many times it is inserted into the document.

If one was wondering what the green tick button did, this adds the name of the User Field into the Select list:

Q2: Why does one need to use variables?

A2: The best example I can think of is with respect to business templates for something like contracts or quotes that may reference a company name more than once throughout the document. You don't need to search and replace all instances of a name; you just update the first (if a variable) or any instance of a User Field to change every place in the document they have been placed.

Q3: Can you insert a field with a formula into a table?

A3: The annoyingly pedantic answer is Yes, but alas, it won't work. This is what will happen:

A3 (cont): The trick is to create the field OUTSIDE the table, and then copy & paste that field into the table. Changing the value 1/12/22 to 4/12/22 will now be reflected in the second row of the table (and the field outside the table can be deleted.)