Skip to main content
Logo image

Section 4.2 Authoring diagrams within PreTeXt

PreTeXt authors will notice that the design of PreFigure is very much inspired by PreTeXt. In fact, it is possible to write PreFigure source directly inside a PreTeXt document. There are just a few things to be aware of.
The diagram in Figure 4.2.1 was created inside a PreTeXt document, as we will explain.
Figure 4.2.1. A PreFigure diagram created inside a PreTeXt document as shown in Listing 4.2.2.
Listing 4.2.2 shows a portion of a PreTeXt document containing PreFigure source. First, notice that we use a PreTeXt <image> element and place a PreTeXt <prefigure> element inside of it. The <prefigure> element needs to have the attribute @xmlns="https://prefigure.org", which serves to effectively isolate any PreFigure source from the rest of the document, and a @label giving the diagram a name.
<image width="60%">
  <prefigure xmlns="https://prefigure.org"
             label="prefigure-rectangle">
    <diagram dimensions="(300,300)" margins="5">
      <coordinates bbox="(0,0,10,10)">
        <grid-axes/>
        <rectangle center="(5,5)" dimensions="(6,6)"/>
        <label anchor="(0.5,0.5)" alignment="ne"
               clear-background="yes">
          The derivative is <m>\deriv{y}{x}</m>.
        </label>
      </coordinates>
    </diagram>
  </prefigure>
</image>
Listing 4.2.2. PreFigure source inside a PreTeXt document.
Rather than a publication file, as described in Section 4.1, default behaviors can be specified in the PreTeXt <docinfo> element as shown in Listing 4.2.3. Including this has the effect of applying the publication file given in Listing 4.1.1.
<docinfo>
  <macros>
    \newcommand{\deriv}[2]{\displaystyle \frac{d#1}{d#2}}
    \newcommand{\real}{\Bbb R}
  </macros>
  <prefigure-preamble xmlns="https://prefigure.org">
    <graph stroke="green"/>
    <point style="diamond" size="5" fill="blue" outline="yes"/>
  </prefigure-preamble>
</docinfo>
Listing 4.2.3. Within a PreTeXt document, PreFigure publication data is included inside <docinfo>.
Notice that the macros defined inside <docinfo/macros>, which are available throughout the entire PreTeXt document, are also made available for creating PreFigure labels. In addition, three more macros will be automatically added:
\newcommand{\lt}{&lt;}
\newcommand{\gt}{&gt;}
\newcommand{\amp}{&amp;}
  
Furthermore, you may add a <prefigure-preamable> element inside <docinfo> to change the default appearance of specified graphical components. The <prefigure-preamble> element needs the same namespace attribute @xmlns="https://prefigure.org" as the <prefigure> element.
The PreTeXt document that created Figure 4.2.1 has a <prefigure-preamble> with a single <rectangle> element having attributes @stroke="black" and @fill="green". The <macros> includes the macro to create the label \(\frac{dy}{dx}\text{.}\)