Skip to main content
Chemistry LibreTexts

6.2: Making Tables with niceTables

  • Page ID
    555724
  • ​

    ​

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\ket}[1]{\left| #1 \right>}\)
    \(\newcommand{\bra}[1]{\left< #1 \right|}\)
    \(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
    \(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
    \(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
    \(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
    \(\newcommand{\expect}[1]{\langle #1 \rangle}\)
    \(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    This page explains how to build tables with niceTables.pl (see Common PG Macros for macro loading), which is the only supported way to create tables in this install. For basic table concepts in PGML, see Tables and structured data. Standard HTML table tags (<table>, <tr>, <td>, <th>) are blocked by the server and will not render. Use DataTable or LayoutTable instead.

    Use this page when: you need to present measurements, observations, or structured data in a readable format inside a WeBWorK problem.

    Why niceTables is the only option

    The WeBWorK PG rendering engine strips raw HTML table tags for security reasons. If you write <table><tr><td>...</td></tr></table> inside a problem, the tags are silently removed and the cell contents collapse into a single line. The niceTables.pl macro generates safe, styled tables that the server allows through. Every table in every problem must use this macro.

    DataTable vs LayoutTable

    DataTable vs LayoutTable: table type, use it for, and notes.
    Table type Use it for Notes
    DataTable Real data with headers, captions, or semantic meaning (measurements, conditions, experimental results). Adds header structure and improves accessibility. Supports the caption option.
    LayoutTable Visual layout: side-by-side answer blanks, aligned labels, widget grids. No header semantics. Use sparingly; do not fake data tables with LayoutTable.

    Rule of thumb: if the content has a header row or a caption, use DataTable. If you are arranging blanks or labels spatially, use LayoutTable.

    Copy and edit (DataTable)

    This template shows a three-row DataTable with a header row. Customize the row data, column headers, and options to match your experiment.

    loadMacros(
      "PGstandard.pl",
      "MathObjects.pl",
      "PGML.pl",
      "niceTables.pl",
    );
    
    $rows = [
      ['Tube', 'Stock (mM)', 'Final (uM)'],
      ['A', 2.0, ''],
      ['B', 4.0, ''],
      ['C', 8.0, ''],
    ];
    
    $tbl = DataTable($rows, columns => 'left', center => 1, horizontalrules => 1);
    
    BEGIN_PGML
    [@ $tbl @]*
    END_PGML
    

    Copy and edit (LayoutTable)

    Use LayoutTable when you need to arrange answer blanks or labels side by side without header semantics. Customize the cell contents and answer variables.

    $layout = LayoutTable(
      [
        [ 'Lane 1', 'Lane 2', 'Lane 3' ],
        [ '[________]{ $ans1 }', '[________]{ $ans2 }', '[________]{ $ans3 }' ],
      ],
      center => 1,
      horizontalrules => 1,
    );
    
    BEGIN_PGML
    [@ $layout @]*
    END_PGML
    

    Cell content formats

    Most cells are plain text strings. Use a hashref or arrayref only when you need per-cell alignment or CSS overrides.

    Each cell in the 2-D array you pass to DataTable or LayoutTable can be one of three shapes.

    • Plain text ('hello') -- most cells. Just pass a string or number.
    • Hashref ({ data => 'hello', halign => 'c' }) -- when you need per-cell options like alignment or CSS.
    • Arrayref ([ 'hello', halign => 'c' ]) -- shorthand for hashref. Item 0 is content; the rest are key/value options.

    Table-level options reference

    These options are passed as key/value pairs after the row data.

    • center -- set to 1 to center the table horizontally.
    • caption -- add a caption above the table (DataTable only).
    • horizontalrules -- set to 1 to draw horizontal lines between rows.
    • valign -- vertical alignment: top, middle, or bottom.
    • padding -- array [vertical, horizontal] specifying cell padding in em.

    CSS hooks for fine-grained styling:

    • tablecss -- CSS applied to the outer table element.
    • datacss -- CSS applied to every data cell.
    • headercss -- CSS applied to every header cell.
    • allcellcss -- CSS applied to every cell (header and data).
    • columnscss -- arrayref of CSS strings, one per column.

    PGML table syntax

    PGML's inline table syntax is convenient for simple tables that do not need heavy customization.

    PGML also supports an inline table syntax using [# ... #]. This is backed by niceTables internally and is acceptable when it improves readability. Each row uses [. ... .] for cells.

    BEGIN_PGML
    [# 'Tube'  , 'Stock (mM)' , 'Final (uM)' #]
    [. 'A'     , 2.0          , [________]{$ans1} .]
    [. 'B'     , 4.0          , [________]{$ans2} .]
    [. 'C'     , 8.0          , [________]{$ans3} .]
    END_PGML
    

    For complex tables with many options, the explicit DataTable() or LayoutTable() call gives you more control.

    Translating HTML tables

    If you are converting a problem from another platform or an older PG format, you will likely encounter raw HTML tables that need translation.

    If you are converting a problem that originally used raw HTML tables, follow these rules.

    • Header row or caption present: use DataTable(). Map <th> cells to the first row of the array.
    • Pure layout (two-column lists, widget grids): use LayoutTable().
    • Nested tables: not supported. Flatten the content into a single table or use separate tables.
    • rowspan or colspan: not supported. Restructure the data into a simple rectangular grid.
    • Never emit raw HTML table tags as a fallback. If the table cannot be translated, rewrite it manually.

    Biology examples

    Dilution series

    A DataTable showing stock concentrations where students compute the final concentration.

    $dilution = DataTable(
      [
        ['Tube', 'Stock (mM)', 'Dilution', 'Final (uM)'],
        ['A', 2.0, '1:10', ''],
        ['B', 4.0, '1:10', ''],
        ['C', 8.0, '1:10', ''],
      ],
      caption => 'Serial dilution worksheet',
      center => 1,
      horizontalrules => 1,
    );
    

    Enzyme kinetics

    Substrate concentration vs reaction velocity with one computed blank.

    $kinetics = DataTable(
      [
        ['[S] (mM)', 'Velocity (umol/min)'],
        [0.1, 12],
        [0.5, 45],
        [1.0, '[________]{ $vmax_half }'],
        [5.0, 88],
      ],
      center => 1,
      horizontalrules => 1,
    );
    

    Gel or blot comparison

    A LayoutTable that arranges lane labels above answer blanks so students identify bands.

    $gel = LayoutTable(
      [
        [ 'Marker', 'Sample 1', 'Sample 2', 'Control' ],
        [ '[________]{ $band1 }', '[________]{ $band2 }',
          '[________]{ $band3 }', '[________]{ $band4 }' ],
      ],
      center => 1,
      allcellcss => 'padding: 4px 8px;',
    );
    

    Complete working example

    Enzyme kinetics data table -- A full problem presenting substrate concentration vs velocity data in a DataTable, with a RadioButtons question about the data pattern.

    ## TITLE('Enzyme kinetics data interpretation')
    ## DESCRIPTION
    ## Review an enzyme kinetics data table and identify the
    ## relationship between substrate concentration and velocity.
    ## ENDDESCRIPTION
    ## KEYWORDS('enzyme kinetics','DataTable','niceTables','RadioButtons')
    ## DBsubject('Biochemistry')
    ## DBchapter('Enzymes')
    ## DBsection('Kinetics')
    ## Date('2026-02-15')
    ## Author('Author name')
    ## Institution('Institution name')
    
    DOCUMENT();
    loadMacros(
    	"PGstandard.pl",
    	"MathObjects.pl",
    	"PGML.pl",
    	"niceTables.pl",
    	"parserRadioButtons.pl",
    	"PGcourse.pl",
    );
    
    $tbl = DataTable(
    	[
    		['[S] (mM)', 'Velocity (umol/min)'],
    		[0.1, 12],
    		[0.5, 45],
    		[1.0, 68],
    		[5.0, 88],
    		[10.0, 95],
    	],
    	center => 1,
    	horizontalrules => 1,
    );
    
    $rb = RadioButtons(
    	[ 'Velocity increases linearly with [S]',
    	  'Velocity approaches a maximum as [S] increases',
    	  'Velocity decreases as [S] increases',
    	  'Velocity is independent of [S]' ],
    	'Velocity approaches a maximum as [S] increases',
    	labels        => 'ABC',
    	displayLabels => 1,
    );
    
    BEGIN_PGML
    The following table shows enzyme kinetics data.
    
    [@ $tbl @]*
    
    Which statement best describes the relationship between
    substrate concentration and reaction velocity?
    
    [_]{$rb}
    END_PGML
    
    ENDDOCUMENT();
    

    Apply it today

    • Use DataTable for real measurements and LayoutTable only for visual arrangement.
    • Never use raw HTML table tags; they are blocked and will not render.
    • Start with the copy-and-edit examples above, then customize options as needed.

    This page titled 6.2: Making Tables with niceTables was last modified on Fri, 27 Feb 2026 15:37:33 GMT and is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Neil R. Voss.