|
1 | 1 | # Syntax Guide |
2 | 2 |
|
| 3 | + |
3 | 4 | ```{note} |
4 | 5 | This documentation utilized the [Markedly Structured Text (MyST)](https://myst-parser.readthedocs.io/en/latest/index.html) syntax. |
5 | 6 | ``` |
6 | 7 |
|
7 | | -## Exercises |
| 8 | +## Exercise Directive |
8 | 9 |
|
9 | 10 | An exercise directive can be included using the `exercise` pattern. The directive is enumerated by default and can take in an optional title argument. The following options are also supported: |
10 | 11 |
|
11 | 12 | * `label` : text |
12 | 13 |
|
13 | | - A unique identifier for your exercise that you can use to reference it with `{ref}`. Cannot contain spaces or special characters. |
| 14 | + A unique identifier for your exercise that you can use to reference it with `{ref}` and `{numref}`. Cannot contain spaces or special characters. |
14 | 15 | * `class` : text |
15 | 16 |
|
16 | 17 | Value of the exercise’s class attribute which can be used to add custom CSS or JavaScript. |
@@ -51,3 +52,133 @@ for any positive integer $n$. |
51 | 52 | `````` |
52 | 53 |
|
53 | 54 | _Source:_ [QuantEcon](https://python-programming.quantecon.org/functions.html#Exercise-1) |
| 55 | + |
| 56 | +### Referencing Exercises |
| 57 | + |
| 58 | +You can refer to an exercise using the `{ref}` role like ```{ref}`my-exercise` ```, which will display the title of the exercise directive. In the event that directive does not have a title, the title will default to "Exercise" like so: {ref}`my-exercise`. |
| 59 | + |
| 60 | +Enumerable directives can also be referenced through the `numref` role like ```{numref}`my-exercise` ```, which will display the number of the exercise directive. Referencing the above directive will display {numref}`my-exercise`. Furthermore, `numref` can take in three additional placeholders: _%s_ and _{number}_ which get replaced by the exercise number and _name_ by the exercise title.[^note] |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +<!-- You can refer to an exercise using the `{ref}` role like: ```{ref}`my-exercise` ```, which will replace the reference with the exercise number like so: {ref}`my-exercise`. When an explicit text is provided, this caption will serve as the title of the reference. --> |
| 65 | + |
| 66 | +[^note]: If the exercise directive does not have a title, an `invalid numfig format` warning will be displayed during build if the user tries to use the _{name}_ placeholder. |
| 67 | + |
| 68 | + |
| 69 | +## Solution Directive |
| 70 | + |
| 71 | +A solution directive can be included using the `solution` pattern. It takes in the label of the directive it wants to link to as a required argument. Unlike the `exercise` directive, the solution directive is unenumerable. The following options are also supported: |
| 72 | + |
| 73 | +* `label` : text |
| 74 | + |
| 75 | + A unique identifier for your solution that you can use to reference it with `{ref}`. Cannot contain spaces or special characters. |
| 76 | +* `class` : text |
| 77 | + |
| 78 | + Value of the solution’s class attribute which can be used to add custom CSS or JavaScript. |
| 79 | + |
| 80 | +```{note} |
| 81 | +The title of the solution directive links directly to the referred directive. |
| 82 | +``` |
| 83 | + |
| 84 | +**Example** |
| 85 | + |
| 86 | +````{solution} my-exercise |
| 87 | +:label: my-solution |
| 88 | +
|
| 89 | +Here's one solution. |
| 90 | +
|
| 91 | +```{code-block} python |
| 92 | +def factorial(n): |
| 93 | + k = 1 |
| 94 | + for i in range(n): |
| 95 | + k = k * (i + 1) |
| 96 | + return k |
| 97 | +
|
| 98 | +factorial(4) |
| 99 | +``` |
| 100 | +```` |
| 101 | + |
| 102 | +**MyST Syntax** |
| 103 | + |
| 104 | +``````md |
| 105 | +````{solution} my-exercise |
| 106 | +:label: my-solution |
| 107 | + |
| 108 | +Here's one solution. |
| 109 | + |
| 110 | +```{code-block} python |
| 111 | +def factorial(n): |
| 112 | + k = 1 |
| 113 | + for i in range(n): |
| 114 | + k = k * (i + 1) |
| 115 | + return k |
| 116 | + |
| 117 | +factorial(4) |
| 118 | +``` |
| 119 | +```` |
| 120 | +`````` |
| 121 | + |
| 122 | +_Source:_ [QuantEcon](https://python-programming.quantecon.org/functions.html#Exercise-1) |
| 123 | + |
| 124 | + |
| 125 | +### Referencing Solutions |
| 126 | + |
| 127 | +You can refer to a solution using the `{ref}` role like: ```{ref}`my-solution` ``` the output of which depends on the attributes of the linked directive. If the linked directive is enumerable, the role will replace the solution reference with the linked directive type and its appropriate number like so: {ref}`my-solution`. |
| 128 | + |
| 129 | +In the event that the directive being referenced is unenumerable, the reference will display its title: {ref}`nfactorial-solution`. Click the toggle to see the supporting directives. |
| 130 | + |
| 131 | +````{toggle} |
| 132 | +
|
| 133 | +```{exercise} $n!$ Factorial |
| 134 | +:label: nfactorial |
| 135 | +:nonumber: |
| 136 | +
|
| 137 | +Write a function `factorial` such that `factorial(int n)` returns $n!$ |
| 138 | +for any positive integer $n$. |
| 139 | +``` |
| 140 | +
|
| 141 | +```{solution} nfactorial |
| 142 | +:label: nfactorial-solution |
| 143 | +
|
| 144 | +Here's a solution in Java. |
| 145 | +
|
| 146 | +```{code-block} java |
| 147 | +static int factorial(int n){ |
| 148 | + if (n == 0) |
| 149 | + return 1; |
| 150 | + else { |
| 151 | + return(n * factorial(n-1)); |
| 152 | + } |
| 153 | +} |
| 154 | +``` |
| 155 | +```` |
| 156 | + |
| 157 | + |
| 158 | +If the title of the linked directive being reference does not exist, it will default to {ref}`nfactorial-notitle-solution`. Click the toggle to see the supporting directives. |
| 159 | + |
| 160 | +````{toggle} |
| 161 | +
|
| 162 | +```{exercise} |
| 163 | +:label: nfactorial-notitle |
| 164 | +:nonumber: |
| 165 | +
|
| 166 | +Write a function `factorial` such that `factorial(int n)` returns $n!$ |
| 167 | +for any positive integer $n$. |
| 168 | +``` |
| 169 | +
|
| 170 | +```{solution} nfactorial-notitle |
| 171 | +:label: nfactorial-notitle-solution |
| 172 | +
|
| 173 | +Here's a solution in Java. |
| 174 | +
|
| 175 | +```{code-block} java |
| 176 | +static int factorial(int n){ |
| 177 | + if (n == 0) |
| 178 | + return 1; |
| 179 | + else { |
| 180 | + return(n * factorial(n-1)); |
| 181 | + } |
| 182 | +} |
| 183 | +``` |
| 184 | +```` |
0 commit comments