You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A restriction of MyST is that `code-cell` directives must be at the root level of the document for them to be executed. This maintains direct
157
+
compatility with the `jupyter notebook` and enables tools like `jupytext` to convert between `myst` and `ipynb` files.
158
+
159
+
As a result **executable**`code-cell` directives cannot be nested inside of exercises or solution directives.
160
+
161
+
The solution to this is to use the **gated syntax**.
162
+
163
+
```{note}
164
+
This syntax can also be a convenient way of surrounding blocks of text that may include other directives that you wish
165
+
to include in an exercise or solution admonition.
166
+
```
167
+
168
+
### Basic Syntax
169
+
170
+
````md
171
+
```{exercise-start}
172
+
:label: ex1
173
+
```
174
+
175
+
```{code-cell}
176
+
# Some setup code that needs executing
177
+
```
178
+
179
+
and maybe you wish to add a figure
180
+
181
+
```{figure} img/example.png
182
+
```
183
+
184
+
```{exercise-end}
185
+
```
186
+
````
187
+
188
+
The `exercise-start` directive allows for he same options as core `exercise` directive.
189
+
190
+
````md
191
+
```{solution-start} ex1
192
+
```
193
+
194
+
```{code-cell}
195
+
# Solution Code
196
+
```
197
+
198
+
```{solution-end}
199
+
```
200
+
````
201
+
202
+
```{warning}
203
+
If there are missing `-start` and `-end` directives, this will cause Sphinx to return an extension error,
204
+
alongside some helpful feedback to diagnose the issue in document structure.
205
+
```
206
+
207
+
### Example
208
+
209
+
````md
210
+
211
+
```{solution-start} exercise-1
212
+
:label: solution-gated-1
213
+
```
214
+
215
+
This is a solution to Exercise 1
216
+
217
+
```{code-cell} python3
218
+
import numpy as np
219
+
import matplotlib.pyplot as plt
220
+
221
+
# Fixing random state for reproducibility
222
+
np.random.seed(19680801)
223
+
224
+
dt = 0.01
225
+
t = np.arange(0, 30, dt)
226
+
nse1 = np.random.randn(len(t)) # white noise 1
227
+
nse2 = np.random.randn(len(t)) # white noise 2
228
+
229
+
# Two signals with a coherent part at 10Hz and a random part
230
+
s1 = np.sin(2 * np.pi * 10 * t) + nse1
231
+
s2 = np.sin(2 * np.pi * 10 * t) + nse2
232
+
233
+
fig, axs = plt.subplots(2, 1)
234
+
axs[0].plot(t, s1, t, s2)
235
+
axs[0].set_xlim(0, 2)
236
+
axs[0].set_xlabel('time')
237
+
axs[0].set_ylabel('s1 and s2')
238
+
axs[0].grid(True)
239
+
240
+
cxy, f = axs[1].cohere(s1, s2, 256, 1. / dt)
241
+
axs[1].set_ylabel('coherence')
242
+
243
+
fig.tight_layout()
244
+
plt.show()
245
+
```
246
+
247
+
With some follow up text to the solution
248
+
249
+
```{solution-end}
250
+
```
251
+
252
+
````
253
+
254
+
will produce the following `solution` block in your `html` output.
255
+
256
+
```{figure} img/gated-directive-example.png
257
+
```
258
+
259
+
154
260
### Referencing Solutions
155
261
156
262
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`.
@@ -277,16 +383,16 @@ Any specific directive can be hidden by introducing the `:hidden:` option. For e
0 commit comments