3.6 Axis labels and titles

Labels can be added to the axes of a plot, and a title put at the top. As with any other strings (see the previous section), labels should be enclosed in either single (’) or double (") quotes, as in the following example script:

set xlabel "Horizontal axis"
set ylabel "Vertical axis"
set title 'A plot with labelled axes'
plot
\includegraphics[width=8cm]{examples/eps/ex_axislabs}

These labels and title – in fact, all text labels which are ever produced by Pyxplot – are rendered using the latex typesetting system, and so any latex commands can be used to produce custom formatting. This allows great flexibility, but means that care needs to be taken to escape any of latex’s reserved characters – i.e. $\backslash $ & % # { } $ _ \^{} or $\sim $.

Two built-in functions provide some assistance in generating latex labels. The texify() function takes as its argument a string containing a mathematical expression, and returns a latex representation of it. The texifyText() function takes as its argument a text string, and returns a latex representation of it, with any necessary escape characters added. For example:

pyxplot> print texify("sqrt(x**2+1)")
$$\backslash $displaystyle $\backslash $sqrt{x$\backslash $,*$\backslash $kern-1.5pt *$\backslash $,$\backslash $,2+1}$\backslash $mathrm{}$
pyxplot> a=50
pyxplot> print texifyText("A %d%% increase"%a)
A 50$\backslash $% increase
pyxplot> set ylabel texify("cos(x**2)")

Special care needs to be taken when typesetting latex expressions that contain apostrophe or quote characters, as these are the string delimiters used by Pyxplot. For ease, it is recommended that latex expressions be enclosed in triple quotes:

\includegraphics[width=0.9cm]{tick.eps}

set xlabel """$\backslash $textrm{J$\backslash $"org’s data}"""

The reason for recommending this syntax is demonstrated by the examples below, all of which will fail. In

\includegraphics[width=0.9cm]{cross.eps}

set xlabel ’My plot’s X axis’

the apostrophe will be mis-interpreted as a closing quote character. In

\includegraphics[width=0.9cm]{cross.eps}

set xlabel "J$\backslash $"org’s data"

the backslash before the " character, intended to be the latex control string for an umlaut ($\backslash $"o), will instead be interpreted as a Pyxplot escape character. It will not be passed to latex, and a latex error will result. Whilst it is possible to write

set xlabel "J\\\"org's data"

this syntax is messy, as the backslashes are confusing to the eye. It is much neater to use (see Section 3.5 for an explanation of string escaping):

\includegraphics[width=0.9cm]{tick.eps}

set xlabel r"""J$\backslash $"org’s data"""

There are similar problems with

\includegraphics[width=0.9cm]{cross.eps}

set xlabel e"2 $\backslash $times 3"

where the $\backslash $t will be turned into a tab character, because extended escape characters are enabled. This string could be made legal by removing the e prefix (see Section 3.5).