2.1: Introduction to PG Language
- Page ID
- 488656
\( \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}\)
PG is the programming language behind every WeBWorK problem. It is implemented in Perl and provides macros for answer checking, randomization, and formatting. Think of PG as the scaffolding that supports the student-facing prompt: it loads the tools you need, sets up variables, and wires the grading, but the text the student actually reads belongs in PGML.
This page covers only the PG you need to support a PGML prompt. If you can read this page, you already know enough PG to write most science and math problems. Set up a local renderer to test your work (Setting Up the PG Renderer).
Use this page when: you need to change setup behavior without touching the student-facing prompt.
The five-component problem structure
Every PG problem file follows the same five components, in order. Keeping this structure consistent makes problems easier to read, debug, and share.
- OPL header: metadata comments for searchability and attribution (covered in OPL Header and Metadata; see also Breaking Down the Components for a detailed component breakdown).
- Preamble:
DOCUMENT();andloadMacros(...). - Setup: context selection, random values, computed answers, and answer checkers.
- Statement: the PGML text block that the student reads and interacts with (see PGML (PG Markup Language) for PGML details).
- Solution: a solution block (and optional hints) for student review.
Canonical problem skeleton
Copy this skeleton and fill in the pieces you need.
## (Tagging header block goes here -- see Section 2.2)
DOCUMENT();
# ----------------------------
# 1) Preamble
# ----------------------------
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"PGcourse.pl",
);
$showPartialCorrectAnswers = 1;
# ----------------------------
# 2) Setup
# ----------------------------
Context("Numeric");
$a = random(2, 9, 1);
$b = random(3, 9, 1);
$ans = Compute("$a + $b");
# ----------------------------
# 3) Statement (PGML)
# ----------------------------
BEGIN_PGML
Compute [`[$a] + [$b]`].
Answer: [__________]{$ans}
END_PGML
# ----------------------------
# 4) Solution
# ----------------------------
BEGIN_PGML_SOLUTION
The answer is [`[$a] + [$b] = [$ans]`].
END_PGML_SOLUTION
ENDDOCUMENT();
Key rules
- Every file must start with
DOCUMENT();and end withENDDOCUMENT();. - Put all macro loads in a single
loadMacros(...)block in the preamble. - All student-facing text goes inside
BEGIN_PGML/END_PGMLblocks. Do not write long prompts in raw PG. - Always include a
BEGIN_PGML_SOLUTION ... END_PGML_SOLUTIONblock, even if it is only a single sentence. - Keep the five sections in order and visually separated with comment headings.
Minimum recommended macros
Start every problem with these four macros. Add others only when you need them.
| Macro | Purpose |
|---|---|
PGstandard.pl |
Core PG functions; required in practice for every problem. |
MathObjects.pl |
Modern answer types such as Real(), Compute(), and Formula(). |
PGML.pl |
Enables BEGIN_PGML / END_PGML blocks for writing prompts. |
PGcourse.pl |
Commonly expected by WeBWorK installations; include unless you have a reason to drop it. |
If you add a specialized macro (for example parserPopUp.pl for dropdowns), add a short comment explaining why.
PGML-first authoring
In PGML-first problems, the student-facing text and answer blanks live entirely inside PGML blocks. PG handles everything that happens before and after the prompt: loading macros, choosing a context, generating random values, and defining correct answers.
Inline grading principle: attach answer evaluators directly to blanks in the PGML block. Do not create a blank in PGML and then wire the grader separately with ANS(...) calls.
# Preferred: evaluator attached at the blank
BEGIN_PGML
Answer: [__________]{$ans}
END_PGML
# Avoid: blank in PGML with separate ANS() wiring
BEGIN_PGML
Answer: [__________]
END_PGML
ANS($ans->cmp());
When the evaluator lives next to the blank, the connection between the prompt and the grading stays obvious. This is especially helpful for problems with multiple answer blanks.
File extensions: .pgml vs .pg
Use the .pgml extension for full PGML-first problems where the statement and graders are entirely inside PGML blocks. If a problem uses legacy ANS(...) wiring or mixed PG/PGML structure, keep the extension as .pg. The extension signals whether tooling should expect pure PGML structure.
Context selection
A context controls what kind of input WeBWorK accepts from the student. Always set an explicit context in the Setup section before defining answers.
| Context | When to use |
|---|---|
Numeric |
General numeric answers (the most common default). |
Real |
Answers that must be real numbers (no complex values). |
Vector |
Answers that are vectors. |
Matrix |
Answers that are matrices. |
Complex |
Answers that involve complex numbers. |
Question order recommendation
Inside the PGML block, present information in this order so that questions are consistent and scannable for students.
- Background: context or scenario the student needs to understand.
- Statement: what is being asked.
- Data: any tables or given values.
- Directions: how to respond, constraints, formatting expectations.
Keep directions concise and place them after the data or statement rather than before the prompt.
PG concepts you actually need
| PG concept | Why you care | What you type |
|---|---|---|
| Macro loading | Enables PGML and answer checking. | loadMacros(...) |
| Context | Controls what student input is allowed. | Context("Numeric") |
| Random values | Creates variants without changing the story. | random(2, 8, 1) |
| MathObjects | Builds answers the grader can check. | Real(...) or Compute(...) |
| Answer checker | Defines how grading works. | $ans->cmp() |
Common failure and fix
Broken: $ans = Real($a + $b); What you will see: Undefined subroutine &main::Real called Fix: load "MathObjects.pl" before using Real(...)
A mindset for science problems
- Randomize the values, not the story.
- Keep units explicit and consistent (and prevent unit drift: μM vs mM, μL vs mL).
- Use realistic ranges (no negative concentrations, no 10,000 μL pipettes).
- For text answers, state naming expectations (gene/protein symbols, case, and hyphens).
- Prefer one or two answer blanks with clear interpretation.
- When you need a table or structured layout, reach for PGML helpers rather than ad-hoc HTML.
Safe defaults (micro-pattern)
- Choose a context early.
- Define a small set of values with constrained ranges.
- Define answers as MathObjects.
- Attach answers directly to blanks in PGML.
You can stop here if you are only editing prompts or adding one numeric value. This page is enough to keep your PG setup stable.
Next, depending on what you are doing:
- Need the OPL header? Go to OPL Header and Metadata.
- Need a clean starter .pg skeleton? Go to PG Problem Files.
- Not sure where setup ends and PGML begins? Go to Sections within a PG question.
- Need macro help? Go to Common PG Macros.


