Skip to main content
Logo image

Section 22 Pre-Formatted Text

View Source for section
<section label="section-pre-formatted">
    <title>Pre-Formatted Text</title>
    <p>
      In Sage, if you wanted to build a matrix,
          <idx>matrix</idx>
      then you would use the <c>matrix()</c> constructor.
      Here is the matrix of second partials of <m>f(x,y)=x^3+8x^2y^3 + y^4</m>,
      as you would enter it in Sage.
      Notice that <c>SR</c> is the ring of symbolic expressions,
      <c>Symbolic Ring</c>.
    </p>
<pre>
var('x', 'y')
J = matrix(SR, [
[6*x + 16*y^3, 48*x*y^2],
[48*x*y^2, 48*x^2*y + 12*y^2]
])
</pre>
    <p>
      That accomplished, Sage will easily and naturally provide a <latex /> representation of the matrix with the command <c>latex(J)</c>.
    </p>
<pre>
\left(\begin{array}{rr}
16 \, y^{3} + 6 \, x &amp; -48 \, x y^{2} \\
48 \, x y^{2} &amp; 48 \, x^{2} y + 12 \, y^{2}
\end{array}\right)
</pre>
    <p>
      The <c>pre</c> element surrounds text that should be preserved verbatim.
      It is like a special kind of paragraph,
      and can be used almost everywhere that a paragraph can be used.
      The realization of preformatted text should be robust enough that it can be cut from documents and pasted without any substitutions of
      <q>fancier</q>
      Unicode characters for generic ASCII characters.
      Try the
      <q>minus</q>
      sign on the <m>48</m> above to see if it does not become a dash,
      or the single quotes on the Sage variables.
    </p>
    <p>
      For Sage input code,
      the first non-whitespace character sets the left margin,
      since legitimate Python code has no subsequent lines outdented.
      For pre-formatted code, the line with the <em>least</em>
      whitespace leading the line will determine the left margin.
      If preserving indentation is important,
      do not mix spaces and tabs.
      For syntax highlighting of text representing computer programs,
      or parts of them,
      see Section<nbsp /><xref ref="section-programs" />.
      Examine the source of the following example to help understand this paragraph.
    </p>
<pre>
A normal line
An indented line
An outdented line
</pre>
    <p>
      Snippets should also be robust for cut/paste operations.
      For example, you should not get
      <q>curly</q>
      <q>smart</q>
      quote marks in verbatim text:
      <c>this should have "dumb" quote marks</c>.
      Here are a few characters that should migrate through <latex /> to a PDF unmolested:
      <c>'"----"'</c>
    </p>
    <p>
      <c>If you write a very long snippet of inline code (i.e. within a &lt;c&gt;
      element) it can impinge on the right margin,
      since very long words will not hypenate,
      unless you have a dash/hypen.
      Such as when you use words like pneumonoultramicroscopicsilicovolcanoconiosis,
      parastratiosphecomyia stratiosphecomyioides,
      floccinaucinihilipilification, or subdermatoglyphic.
      For output in LaTeX we get line-breaking, and perhaps word-spacing,
      but we do not get hyphenation and the font is fixed-width.
      So not always perfect.
      Consider other options like &lt;cd&gt; or &lt;pre&gt;
      below.</c>
    </p>
    <p>
      An intermediate type of verbatim text can be accomplished with the <tag>cd</tag> tag,
      short for
      <q>code display.</q>
      It allows for larger chunks of verbatim text to show up in the middle of a paragraph,
      but with some vertical space above and below,
      and centered between the margins.
      It can be
      <cd>authored as a single line</cd>
      or if you wish to have multiple lines
      <cd>
        <cline>there is the &lt;cline&gt; tag</cline>
        <cline>meant to model the line tag</cline>
        <cline>and short for "code line"</cline>
      </cd>
      and you may even
      <cd>
        <cline>use a single cline</cline>
      </cd>
      if you like to have your source closely model the visual look of the output.
    </p>
    <p>
      With the <attr>showspaces</attr> attribute of <tag>cd</tag> set to <c>all</c> there will be a visual indication of every space character,
      which is nice if indentation is critical.
      For example,
      <cd showspaces="all">
        <cline>there is the &lt;cline&gt; tag</cline>
        <cline>meant to model the line tag</cline>
        <cline>and short for "code line"</cline>
      </cd>
      and as single line
      <cd showspaces="all">authored as a single line</cd>
      that is not structured with <tag>cline</tag> elements.
    </p>
    <p>
      The <tag>pre</tag> tag is meant for use outside of paragraphs,
      but is otherwise very similar.
      The source may also be structured as a sequence of <tag>cline</tag> as in the next example,
      recycling content from above.
    </p>
<pre>
<cline>If you write a very long snippet of inline code (i.e. within</cline>
<cline>a &lt;c&gt; element) it can impinge on the right margin, since</cline>
<cline>very long words will not hypenate, unless you have a dash/hypen.</cline>
<cline>Such as when you use words like</cline>
<cline>pneumonoultramicroscopicsilicovolcanoconiosis,</cline>
<cline>parastratiosphecomyia stratiosphecomyioides,</cline>
<cline>floccinaucinihilipilification, or subdermatoglyphic. For output</cline>
<cline>in LaTeX we get line-breaking, and perhaps word-spacing, but we</cline>
<cline>do not get hyphenation and the font is fixed-width. So not always</cline>
<cline>perfect. Consider other options like &lt;cd&gt; or &lt;pre&gt; below.</cline>
</pre>
    <p>
      We use a Unicode right arrow (Unicode Character 'RIGHTWARDS ARROW', U+2192) to sometimes indicate the truncation of long lines in a text file.
      It is available in our usual monospace font for <latex />/PDF, but we include a use here in order to make certain that is always the case.
      Here: <c>→</c>.
    </p>
  </section>
In Sage, if you wanted to build a matrix, then you would use the matrix() constructor. Here is the matrix of second partials of \(f(x,y)=x^3+8x^2y^3 + y^4\text{,}\) as you would enter it in Sage. Notice that SR is the ring of symbolic expressions, Symbolic Ring.
var('x', 'y')
J = matrix(SR, [
[6*x + 16*y^3, 48*x*y^2],
[48*x*y^2, 48*x^2*y + 12*y^2]
])
That accomplished, Sage will easily and naturally provide a representation of the matrix with the command latex(J).
\left(\begin{array}{rr}
16 \, y^{3} + 6 \, x & -48 \, x y^{2} \\
48 \, x y^{2} & 48 \, x^{2} y + 12 \, y^{2}
\end{array}\right)
The pre element surrounds text that should be preserved verbatim. It is like a special kind of paragraph, and can be used almost everywhere that a paragraph can be used. The realization of preformatted text should be robust enough that it can be cut from documents and pasted without any substitutions of “fancier” Unicode characters for generic ASCII characters. Try the “minus” sign on the \(48\) above to see if it does not become a dash, or the single quotes on the Sage variables.
For Sage input code, the first non-whitespace character sets the left margin, since legitimate Python code has no subsequent lines outdented. For pre-formatted code, the line with the least whitespace leading the line will determine the left margin. If preserving indentation is important, do not mix spaces and tabs. For syntax highlighting of text representing computer programs, or parts of them, see Section 23. Examine the source of the following example to help understand this paragraph.
A normal line
An indented line
An outdented line
Snippets should also be robust for cut/paste operations. For example, you should not get “curly” “smart” quote marks in verbatim text: this should have "dumb" quote marks. Here are a few characters that should migrate through to a PDF unmolested: '"----"'
If you write a very long snippet of inline code (i.e. within a <c> element) it can impinge on the right margin, since very long words will not hypenate, unless you have a dash/hypen. Such as when you use words like pneumonoultramicroscopicsilicovolcanoconiosis, parastratiosphecomyia stratiosphecomyioides, floccinaucinihilipilification, or subdermatoglyphic. For output in LaTeX we get line-breaking, and perhaps word-spacing, but we do not get hyphenation and the font is fixed-width. So not always perfect. Consider other options like <cd> or <pre> below.
An intermediate type of verbatim text can be accomplished with the <cd> tag, short for “code display.” It allows for larger chunks of verbatim text to show up in the middle of a paragraph, but with some vertical space above and below, and centered between the margins. It can be
authored as a single line
or if you wish to have multiple lines
there is the <cline> tag
meant to model the line tag
and short for "code line"
and you may even
use a single cline
if you like to have your source closely model the visual look of the output.
With the @showspaces attribute of <cd> set to all there will be a visual indication of every space character, which is nice if indentation is critical. For example,
there␣is␣the␣<cline>␣tag
meant␣to␣model␣the␣line␣tag
and␣short␣for␣"code␣line"
and as single line
authored␣as␣a␣single␣line
that is not structured with <cline> elements.
The <pre> tag is meant for use outside of paragraphs, but is otherwise very similar. The source may also be structured as a sequence of <cline> as in the next example, recycling content from above.
If you write a very long snippet of inline code (i.e. within
a <c> element) it can impinge on the right margin, since
very long words will not hypenate, unless you have a dash/hypen.
Such as when you use words like
pneumonoultramicroscopicsilicovolcanoconiosis,
parastratiosphecomyia stratiosphecomyioides,
floccinaucinihilipilification, or subdermatoglyphic. For output
in LaTeX we get line-breaking, and perhaps word-spacing, but we
do not get hyphenation and the font is fixed-width. So not always
perfect. Consider other options like <cd> or <pre> below.
We use a Unicode right arrow (Unicode Character ’RIGHTWARDS ARROW’, U+2192) to sometimes indicate the truncation of long lines in a text file. It is available in our usual monospace font for /PDF, but we include a use here in order to make certain that is always the case. Here: .