First Decision Rule

Write a publisher's house style rule and explain why 2 lines keep a heading on the page and 1 line moves it.

Write a house style rule for a heading near the bottom of a page. A section heading stays when at least 2 lines are left below it, and a chapter heading always starts a new page. Then check 2 lines, 1 line, and a chapter heading.

Run the examples in the Playground. For a terminal, install Native or npm as shown in First Program.

Write and run the rule

Save this complete program as house-style.lspx.

LISPEX
(define (heading-placement lines-left chapter-heading?)
  (if (and (>= lines-left 2) (not chapter-heading?))
      '(decision keep)
      '(decision next-page)))

(heading-placement 5 #f)

With Native or npm installed, run it from the directory containing the file.

SH
lispex house-style.lspx

Output

OUTPUT
(decision keep)

The command exits with status 0 and creates no files. In the Playground, paste and run the same program to see the same answer.

The two inputs describe the heading.

  • lines-left is the number of lines left on the page below the heading.
  • chapter-heading? is #t for a chapter heading and #f for a section heading.

This example expects a valid non-negative line count and a boolean. The calling application checks those inputs before calling the rule. Handling Errors covers invalid inputs.

Five lines leave room for text below the heading, and #f means a section heading. The answer is a list. Its keep symbol tells the application to leave the heading on this page.

Check 2 lines and 1 line

Keep the definition in house-style.lspx. Replace its final call with these three calls, collected in one list.

LISPEX
(list (heading-placement 2 #f)
      (heading-placement 1 #f)
      (heading-placement 5 #t))

Run lispex house-style.lspx again, or run the updated program in the Playground.

Output

OUTPUT
((decision keep) (decision next-page) (decision next-page))

The command again exits with status 0 and creates no files. Read the answers in the same order as the calls.

CaseWhy the rule gives this answerDecision
2 lines, section heading>= includes 2keep
1 line, section heading1 is less than 2next page
5 lines, chapter heading(not chapter-heading?) is falsenext page

Changing >= to > also moves the 2-line case to the next page. That case reveals whether the boundary value is included.

The rule uses next-page for both reasons to move a heading. To distinguish “not enough room” from “chapter heading,” give them separate branches and labels.

How the conditions work

and checks (>= lines-left 2) first. If it is false, and stops. Otherwise, it checks (not chapter-heading?). if evaluates only the selected branch. The quote keeps the chosen decision list as data.

The typesetting application counts lines-left while it lays out the page. It then passes that value to the rule. With the same two values and the same stated execution conditions, this rule produces the same result.

The publisher's editors decide the house style and review the rule. The application checks where its input data came from and decides whether to apply the answer to the page.

Exercise

Keep a section heading on the page only when at least 3 lines are left. Check the smallest count that keeps the heading and the largest count that moves it.

Show one answer

In house-style.lspx, change (>= lines-left 2) to (>= lines-left 3). Replace the final list of calls with this one.

LISPEX
(list (heading-placement 3 #f)
      (heading-placement 2 #f))

Run lispex house-style.lspx again.

Output

OUTPUT
((decision keep) (decision next-page))

The command exits with status 0 and creates no files.

Choose where to run it next

Once you can explain the 2-line and 1-line answers, continue to Run Lispex in Your Application.

For other tasks, choose a guide.