Skip to content

Feature request: collapse solutions by default and/or move solutions to an appendix at build time #84

Description

@mmcky

Context

v1.2.0 added exercise_style = "solution_follow_exercise" (see the v1.2.0 release notes), which simplifies solution titles when solutions are written directly after their exercise. This is a convenient authoring layout — the solution lives next to the exercise in the source, which is easy to write and maintain.

Feedback

We've had reader feedback on a book built with this style (see https://quantecon.github.io/book-dp1/ch-mdps/#ex-mdps-auto-6):

Currently solutions to exercises sit immediately after exercises, making it too tempting to look at the solution. Would it be possible to have them folded by default and open in a drop-down, similar to the lectures?

The key point is that the best layout for authoring (solution next to exercise) is not necessarily the best layout for reading (solution out of sight until wanted). The extension could let authors keep writing solutions inline while choosing a different presentation at publish time.

Proposed options

Two complementary options, both configured in conf.py / _config.yml and only affecting build output, not the source:

Option Behaviour
Collapsed solutions (e.g. solution_collapsed = True or a :class: dropdown-style default) Render each {solution} directive folded by default, expandable via a drop-down, similar to the dropdown admonition in QuantEcon lectures. Keeps the solution adjacent to the exercise but out of immediate view.
Solution appendix (e.g. solution_placement = "appendix") At build time, lift all {solution} directives out of their in-document position and collect them into a generated solutions section / appendix (per document, or a single site-wide page). The exercise would get a link to its solution and the solution would link back to the exercise, as it does today with the default exercise_style.

These are independent: a project could use either, or both (collapsed solutions inside an appendix).

Notes and open questions

  • hide_solutions = True already exists for removing solutions entirely; these options sit between "inline" and "hidden".
  • For the collapsed option, HTML builders can use the existing sphinx-togglebutton / sphinx-design dropdown mechanics; non-HTML builders (LaTeX/PDF) should fall back to rendering the solution inline or in the appendix.
  • For the appendix option, numbering and cross-references ({ref}, {numref}) need to keep resolving to the moved node. The existing order-validation logic from FEAT: Add solution_after_exercise style option #81 would need to be bypassed or adapted when solutions are relocated.
  • Should the appendix be per-document (end of each page) or a single project-wide page? Probably worth supporting both via the config value.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions