Debian Codegraph @

texmf-dist/source/latex/didactic/didactic.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2019--2020, 2022, 2024 by Daniel Bosk <[email protected]>
4 % -------------------------------------------------------
5 %
6 % This file may be distributed and/or modified under the
7 % conditions of the LaTeX Project Public License, either version 1.3c
8 % of this license or (at your option) any later version.
9 % The latest version of this license is in:
10 %
11 % http://www.latex-project.org/lppl.txt
12 %
13 % and version 1.3 or later is part of all distributions of LaTeX
14 % version 2005/12/01 or later.
15 %
16 % \fi
17 %
18 % \iffalse
19 %<*driver>
20 \ProvidesFile{didactic.dtx}
21 %</driver>
22 %<package>\NeedsTeXFormat{LaTeX2e}
23 %<package>\ProvidesPackage{didactic}
24 %<*package>
25 [2024/04/08 v1.7 didactic]
26 %</package>
27 %<package>\RequirePackage{xparse}
28 %<package>\RequirePackage{xkeyval}
29 %<package>\RequirePackage{xstring}
30 %<package>\RequirePackage{etoolbox}
31 %<package>\RequirePackage{pythontex}
32 %<package>\RequirePackage{minted}
33 %<package>\RequirePackage{babel}
34 %<package>\RequirePackage{translations}
35 %<package>\RequirePackage{amsthm}
36 %<package>\RequirePackage{thmtools}
37 %<package>\PassOptionsToPackage{unq}{unique}
38 %<package>\RequirePackage{unique}
39 %
40 %<*driver>
41 \documentclass{ltxdoc}
42 \usepackage[utf8]{inputenc}
43 \usepackage[swedish,british]{babel}
44 \usepackage{pythontex}
45 \usepackage[notheorems]{beamerarticle}
46 \usepackage[marginparmargin=left]{didactic}
47 \usepackage{lipsum}
48 \usepackage{hyperref}
49 \usepackage{cleveref}
50 \EnableCrossrefs
51 \CodelineIndex
52 \RecordChanges
53 \begin{document}
54 \DocInput{didactic.dtx}
55 \PrintChanges
56 %\PrintIndex
57 \end{document}
58 %</driver>
59 % \fi
60 %
61 % \CheckSum{0}
62 %
63 % \CharacterTable
64 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
65 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
66 % Digits \0\1\2\3\4\5\6\7\8\9
67 % Exclamation \! Double quote \" Hash (number) \#
68 % Dollar \$ Percent \% Ampersand \&
69 % Acute accent \' Left paren \( Right paren \)
70 % Asterisk \* Plus \+ Comma \,
71 % Minus \- Point \. Solidus \/
72 % Colon \: Semicolon \; Less than \<
73 % Equals \= Greater than \> Question mark \?
74 % Commercial at \@ Left bracket \[ Backslash \\
75 % Right bracket \] Circumflex \^ Underscore \_
76 % Grave accent \` Left brace \{ Vertical bar \|
77 % Right brace \} Tilde \~}
78 %
79 %
80 % \changes{v1.0}{2019/01/29}{Initial version}
81 % \changes{v1.1}{2019/03/26}{Adds summary environment}
82 % \changes{v1.2}{2019/10/26}{Adds lightblock, darkblock and coloured blocks}
83 % \changes{v1.3}{2019/01/29}{Renames to didactic; adds tools for code examples}
84 % \changes{v1.4}{2023/03/20}{%
85 % Adds settings for memoir and beamer, contrasting code examples,
86 % translations.
87 % }
88 % \changes{v1.5}{2023/03/25}{Adds |\ProvideSemanticEnv|}
89 % \changes{v1.6}{2024/04/07}{%
90 % Sets beamer bibliography item correctly.
91 % Adds |fullwidth| environment, handles margins better.
92 % Adds |sidecaption| environment and |\flushscap| command.
93 % }
94 % \changes{v1.7}{2024/04/08}{%
95 % Adds |\NewNoteType| that improves |\newnotetype| with toggles.
96 % }
97 %
98 % \GetFileInfo{didactic.dtx}
99 %
100 % \DoNotIndex{\newcommand,\newenvironment}
101 %
102 %
103 % \title{The \textsf{didactic} package\thanks{This document
104 % corresponds to \textsf{didactic}~\fileversion, dated \filedate.
105 % Licensed under the terms of LPPL, version 1.3c or later.}}
106 % \author{Daniel Bosk\\\texttt{[email protected]}}
107 %
108 % \maketitle
109 %
110 % \tableofcontents
111 %
112 % \section{Introduction}
113 %
114 % This package introduces some environments that are useful for writing
115 % teaching material.
116 % It also provides a few settings for |beamer| and |memoir| to make the
117 % resulting document more readable.
118 %
119 % \section{Usage}
120 %
121 % The package automatically detects if |beamer| or |memoir| is loaded and
122 % customizes the document accordingly.
123 % \DescribeEnv{nobeamer}
124 % \DescribeEnv{noarticle}
125 % \DescribeEnv{nomemoir}
126 % That can be prevented, however, by using the |nobeamer| or |nomemoir|
127 % options.
128 % The |noarticle| is to prevent the article versions of the (|beamer|)
129 % environments to be defined.
130 %
131 % \DescribeEnv{notheorems}
132 % The package also provides a few environments for |beamer| and |article| mode.
133 % Particularly, it can define theorems and definitions, which might be defined
134 % also by other packages.
135 % To prevent this, use the |notheorems| option.
136 %
137 % \DescribeEnv{inner}
138 % \DescribeEnv{outer}
139 % \DescribeEnv{top}
140 % \DescribeEnv{bottom}
141 % When the package customizes |memoir| it sets the inner and outer margins to
142 % fit margin notes better.
143 % The |inner| and |outer| options can be used to set the inner and outer
144 % margins to be used.
145 % The |top| and |bottom| options can be used to set the top and bottom margins.
146 %
147 % \subsection{Beamer blocks and semantic environments for papers}
148 %
149 % \DescribeEnv{assumption}
150 % \DescribeEnv{idea}
151 % \DescribeEnv{question}
152 % \DescribeEnv{exercise}
153 % \DescribeEnv{activity}
154 % \DescribeEnv{remark}
155 % These environments provides wrappers around Beamer's |block| environment.
156 % The idea is to complement the blocks provided by Beamer, such as |theorem|,
157 % |definition| and |example|, with some more useful blocks.
158 % Each of them provides a block with an appropriate title and optional subtitle
159 % and is of a suitable colour.
160 % For instance, consider the following.
161 % \input{idea.tex}
162 % That can be produced by the following code.
163 % \inputminted[linenos]{latex}{idea.tex}
164 %
165 % \DescribeMacro{\ProvideSemanticEnv}
166 % We can create new such block environments, that work both with |beamer| and
167 % without, by using the |\ProvideSemanticEnv| command.
168 % This command takes five arguments:
169 % \begin{enumerate}
170 % \item the name of the environment,
171 % \item the name of the block environment to use (optional),
172 % \item the title of the block,
173 % \item options to pass to |thmtools| (optional),
174 % \item the key of the translation of the refname of the block,
175 % \item the key of the translation of the refnames of the block.
176 % \item the key of the translation of the Refname of the block,
177 % \item the key of the translation of the Refnames of the block.
178 % \end{enumerate}
179 % For example, the |idea| block above was created by the following code:
180 % \inputminted{latex}{ProvideSemanticEnv.tex}
181 %
182 % \DescribeEnv{lightblock}
183 % \DescribeEnv{darkblock}
184 % We provide two shades of boxes, light coloured and dark coloured boxes.
185 % These can be used to create boxes of various colours easily.
186 % These two environments take two mandatory arguments: the first one is a
187 % colour, the second is the title.
188 %
189 % In the text (not slides) the blocks are not coloured.
190 % For instance, consider the following.
191 % \input{lightblock.tex}
192 % That can be produced by the following code.
193 % \inputminted[linenos]{latex}{lightblock.tex}
194 %
195 % \DescribeEnv{blackblock}
196 % \DescribeEnv{whiteblock}
197 % \DescribeEnv{bwblock}
198 % For instance, we provide a black block, white block and a black-white block.
199 % These take only one mandatory argument: a title.
200 %
201 % \DescribeEnv{redblock}
202 % \DescribeEnv{blueblock}
203 % \DescribeEnv{purpleblock}
204 % \DescribeEnv{greenblock}
205 % \DescribeEnv{yellowblock}
206 % \DescribeEnv{orangeblock}
207 % We also provide a few predefined blocks in various common colours.
208 % They all take a title as mandatory argument.
209 %
210 % \subsection{Tools for code examples}
211 %
212 % We also provide two commands for working with code examples: |lstexample| and
213 % |runpython|.
214 %
215 % \DescribeMacro{\lstexample}
216 % The |\lstexample| command allows us to typeset the source code of the
217 % example.
218 % For instance, |\lstexample{python}{hello.py}| produces the following:
219 % \lstexample{python}{hello.py}
220 %
221 % We can also pass optional arguments directly to |minted| that is used to
222 % typeset the code.
223 % \begin{example}
224 % |\lstexample[linenos,highlightlines=4]{python}{hello.py}| produces the
225 % following.
226 % \lstexample[linenos,highlightlines=4]{python}{hello.py}
227 % \end{example}
228 %
229 % \DescribeMacro{\runpython}
230 % We can also run the example code and include its output using the
231 % |\runpython| macro.
232 % \begin{example}
233 % For instance, |\runpython{hello.py}| produces the following.
234 % \runpython{hello.py}
235 % \end{example}
236 %
237 % \DescribeMacro{\codebycode}
238 % The |\codebycode| command is simply two |\lstexample| commands side by side:
239 % |\codebycode[opt1]{lang1}{file1}[opt2]{lang2}{file2}|.
240 % \codebycode
241 % [breaklines,linenos]{python}{hello.py}
242 % [breaklines,linenos,highlightlines=4]{python}{hello.py}
243 %
244 % \DescribeMacro{\runbyrun}
245 % We can also put the output side by side using
246 % |\runbyrun{hello.py}{hello.py}|.
247 % \runbyrun{hello.py}{hello.py}
248 %
249 % \subsection{Adding margin notes of different kinds}
250 %
251 % \DescribeMacro{\indentedmarginpar}
252 % We want to be able to put quite some content in the margins.
253 % For this reason we want to have indented paragraphs in the margin notes.
254 % We can use the |\indentedmarginpar{text}| command to do this.
255 % For instance, consider the output in the margin.
256 % \indentedmarginpar{%
257 % This is an example of an indented margin paragraph.
258 %
259 % It can contain multiple paragraphs.
260 % }
261 %
262 % \DescribeMacro{\newnotetype}
263 % \DescribeMacro{\ltnote}
264 % We also want to be able to use titled notes of different kinds.
265 % We can use the |\newnotetype[fmtcmd]{type}{title}| command to define a new
266 % type of note.
267 % For instance |\ltnote| is defined as
268 % \begin{center}
269 % |\newnotetype{\ltnote}{\GetTranslationWarn{Learning theory}}|.
270 % \end{center}
271 % \ltnote{This is an example of a learning theory note.}
272 %
273 % We can also change the formatting of the title.
274 % |\newnotetype[\textbf]{\bfnote}{Bold note}| and
275 % |\bfnote{This is a bold note.}| gives us the note in the margin.
276 % \newnotetype[\textbf]{\bfnote}{Bold note}
277 % \bfnote{This is a bold note.}
278 % \ltnote{Another example of a learning theory note.}
279 %
280 % \DescribeMacro{\NewNoteType}
281 % We also have |\NewNoteType| which is almost synonymous to |\newnotetype|.
282 % The difference is that |\NewNoteType| doesn't take a command as argument, but
283 % just a name:
284 % |\NewNoteType{ltnote}{Learning theory}| instead.
285 % Then we also get |\ltnoteoff| to turn off the note and |\ltnoteon| to turn it
286 % back on.
287 %
288 % Notes are off by default in |beamer|, but on by default otherwise.
289 %
290 % \StopEventually{}
291 %
292 % \section{Implementation}
293 %
294 % Let's start with the options.
295 %
296 % \subsection{Options}
297 %
298 % We have a few negative options, that is, if specified we don't want to do
299 % them.
300 % This means that we'll need to use conditionals.
301 % So each option definition consists first of a |\newif| then setting it to
302 % true, and finnally, a |\DeclareOption|.
303 % The body of the |\DeclareOption| then sets the |\if| to false.
304 % After all the options are declared we'll use |\ProcessOptions|.
305 % After that we can have all code actually doing the work, all wrapped in |\if|
306 % statements.
307 %
308 % \begin{environment}{nobeamer}
309 % \begin{environment}{nobeamertheme}
310 % \begin{environment}{noarticle}
311 % This option is used to disable the |beamer| specific parts of the package.
312 % Meaning that we don't customize |beamer| even when we find that it is loaded.
313 % \begin{macrocode}
314 \newif\if@didactic@beamer
315 \@ifclassloaded{beamer}{\@didactic@beamertrue}{\@didactic@beamerfalse}
316 \DeclareOptionX{nobeamer}{\@didactic@beamerfalse}
317 \newif\if@didactic@article
318 \@ifclassloaded{beamer}{\@didactic@articlefalse}{\@didactic@articletrue}
319 \newif\if@didactic@beamertheme
320 \@didactic@beamerthemetrue
321 \DeclareOptionX{nobeamertheme}{\@didactic@beamerthemefalse}
322 \DeclareOptionX{noarticle}{\@didactic@articlefalse}
323 % \end{macrocode}
324 % \end{environment}
325 % \end{environment}
326 % \end{environment}
327 %
328 % \begin{environment}{nomemoir}
329 % This option is used to disable the |memoir| specific parts of the package.
330 % Meaning that we don't customize |memoir| even when we find that it is loaded.
331 % \begin{macrocode}
332 \newif\if@didactic@memoir
333 \@ifclassloaded{memoir}{\@didactic@memoirtrue}{\@didactic@memoirfalse}
334 \DeclareOptionX{nomemoir}{\@didactic@memoirfalse}
335 % \end{macrocode}
336 % \end{environment}
337 %
338 % \begin{environment}{notheorems}
339 % This option is used to disable defining the usual environments provided by
340 % |amsmath|: definition, theorem, etc.
341 % \begin{macrocode}
342 \newif\if@didactic@theorems
343 \@didactic@theoremstrue
344 \DeclareOptionX{notheorems}{\@didactic@theoremsfalse}
345 % \end{macrocode}
346 % \end{environment}
347 %
348 % \begin{environment}{inner}
349 % \begin{environment}{outer}
350 % \begin{environment}{top}
351 % \begin{environment}{bottom}
352 % We also have some key--value options to control the margins.
353 % \begin{macrocode}
354 \newcommand\@didactic@margin@inner{20mm}
355 \newcommand\@didactic@margin@outer{70mm}
356 \newcommand\@didactic@margin@top{25mm}
357 \newcommand\@didactic@margin@bottom{40mm}
358 \DeclareOptionX{inner}[25mm]{\renewcommand\@didactic@margin@inner{#1}}
359 \DeclareOptionX{outer}[65mm]{\renewcommand\@didactic@margin@outer{#1}}
360 \DeclareOptionX{top}[25mm]{\renewcommand\@didactic@margin@top{#1}}
361 \DeclareOptionX{bottom}[40mm]{\renewcommand\@didactic@margin@bottom{#1}}
362 % \end{macrocode}
363 % \end{environment}
364 % \end{environment}
365 % \end{environment}
366 % \end{environment}
367 %
368 % \begin{environment}{marginparmargin}
369 % We also have an option to set the marginpar margin.
370 % We simply pass this on to |memoir| and keep a record ourselves too.
371 % We know that the default in |memoir| is |outer|.
372 % \begin{macrocode}
373 \newcommand\@didactic@marginparmargin{outer}
374 \DeclareOptionX{marginparmargin}{%
375 \renewcommand\@didactic@marginparmargin{#1}
376 \@ifclassloaded{memoir}{%
377 \marginparmargin{#1}
378 \strictpagechecktrue
379 \checkandfixthelayout
380 }{}
381 }
382 % \end{macrocode}
383 % \end{environment}
384 %
385 % Now that we've declared the options we can process them.
386 % \begin{macrocode}
387 \ProcessOptionsX\relax
388 % \end{macrocode}
389 %
390 % \subsection{Customizing \texttt{beamer}}
391 %
392 % If |beamer| is loaded we want to customize it.
393 % However, only if the user hasn't disabled this with the |nobeamer| option.
394 % \begin{macrocode}
395 \if@didactic@beamer
396 % \end{macrocode}
397
398 % If we use |beamer| we want to use the Berlin theme.
399 % This theme guides the viewer very nicely, how must is left and where we are
400 % in the presentation.
401 % (The same is true for the presenter.)
402 % \begin{macrocode}
403 \if@didactic@beamertheme
404 \usetheme{Berlin}
405 % \end{macrocode}
406 % However, we want to customize the footline.
407 % We want to add two lines in the footline.
408 % We want to add the author (left) and institute (right) to the first line.
409 % Then we add the title (left) and page number (right) to the bottom line.
410 % \begin{macrocode}
411 \setbeamertemplate{footline}%{miniframes theme}
412 {%
413 \begin{beamercolorbox}[colsep=1.5pt]{upper separation line foot}
414 \end{beamercolorbox}
415 \begin{beamercolorbox}[ht=2.5ex,dp=1.125ex,%
416 leftskip=.3cm,rightskip=.3cm plus1fil]{author in head/foot}%
417 \leavevmode{\usebeamerfont{author in head/foot}\insertshortauthor}%
418 \hfill%
419 {\usebeamerfont{institute in head/foot}%
420 \usebeamercolor[fg]{institute in head/foot}\insertshortinstitute}%
421 \end{beamercolorbox}%
422 \begin{beamercolorbox}[ht=2.5ex,dp=1.125ex,%
423 leftskip=.3cm,rightskip=.3cm plus1fil]{title in head/foot}%
424 {\usebeamerfont{title in head/foot}\insertshorttitle}%
425 \hfill%
426 \insertframenumber%
427 \end{beamercolorbox}%
428 \begin{beamercolorbox}[colsep=1.5pt]{lower separation line foot}
429 \end{beamercolorbox}
430 }
431 % \end{macrocode}
432 %
433 % We also want to set the transparency of the covered items.
434 % We also want to set the bibliography item to text, we need this to get proper
435 % references (not icons) in the bibliography.
436 % (We need that for certain bibliography styles.)
437 % \begin{macrocode}
438 \setbeamercovered{transparent}
439 \setbeamertemplate{bibliography item}{\relax}
440 \fi
441 % \end{macrocode}
442 %
443 % Finally, we want to add a table of contents at the beginning of each section
444 % and subsection.
445 % We want these to be shaded, so that the current section is highlighted.
446 % We also want to hide the subsections of the other sections.
447 % \begin{macrocode}
448 \AtBeginSection[]{%
449 \begin{frame}<beamer>
450 \tableofcontents[currentsection,
451 subsectionstyle=show/show/hide]
452 \end{frame}
453 }
454 \AtBeginSubsection[]{%
455 \begin{frame}<beamer>
456 \tableofcontents[currentsection,
457 subsectionstyle=show/shaded/hide]
458 \end{frame}
459 }
460 \fi% end \if@didactic@beamer
461 % \end{macrocode}
462 %
463 % \subsection{Customizing \texttt{memoir}}
464 %
465 % If |memoir| is loaded we want to customize it.
466 % However, only if the user hasn't disabled this with the |nomemoir| option.
467 % \begin{macrocode}
468 \if@didactic@memoir
469 % \end{macrocode}
470 %
471 % We want to set up |memoir| to use the Tufte style.
472 % This means that we want to put a lot of text in the margin.
473 % We'll use margin notes and put all the footnotes in the margin.
474 % We also want to use footnotes for references so that they also appear in the
475 % margin.
476 %
477 % We want to use the |marginfix| package to fix the margin notes.
478 % \begin{macrocode}
479 \RequirePackage{marginfix}
480 \setlrmarginsandblock{\@didactic@margin@inner}
481 {\@didactic@margin@outer}
482 {*}
483 \setulmarginsandblock{\@didactic@margin@top}
484 {\@didactic@margin@bottom}
485 {*}
486
487 \footnotesinmargin
488
489 \RequirePackage{ragged2e}
490 \renewcommand{\sidefootform}{\RaggedRight}
491 \renewcommand{\foottextfont}{\footnotesize\RaggedRight}
492
493 \setmpjustification{\RaggedRight}{\RaggedRight}
494
495 % margin figure and caption typeset ragged against text block
496 \setfloatadjustment{marginfigure}{\mpjustification}
497 \setmarginfloatcaptionadjustment{figure}{\captionstyle{\mpjustification}}
498
499 % side captions
500 % https://tex.stackexchange.com/a/275626/17418
501 \sidecapmargin{outer}
502 \setsidecappos{t}
503 \checkandfixthelayout
504 \setsidecaps{\marginparsep}{\marginparwidth}
505 \renewcommand{\sidecapstyle}{%
506 \captionstyle{\RaggedRight}
507 }
508
509 % From https://tex.stackexchange.com/a/324757/17418
510 % Palatino for main text and math
511 \RequirePackage[osf,sc]{mathpazo}
512 % Helvetica for sans serif
513 % (scaled to match size of Palatino)
514 \RequirePackage[scaled=0.90]{helvet}
515 % Bera Mono for monospaced
516 % (scaled to match size of Palatino)
517 \RequirePackage[scaled=0.85]{beramono}
518 \setlxvchars\setxlvchars
519 \checkandfixthelayout
520 \nouppercaseheads
521 % \end{macrocode}
522 %
523 % We also want to adapt the citation commands of a few packages to use
524 % footnotes.
525 % For these, we check if the package is loaded, if it is, we do the changes.
526 % We don't load the packages ourselves, we assume the user has done that.
527 % This also means that the |didactic| package should be loaded last.
528 % \begin{macrocode}
529 \@ifpackageloaded{biblatex}{%
530 \ExecuteBibliographyOptions{%
531 autocite=footnote,
532 singletitle=false,
533 %style=verbose,
534 %citestyle=verbose,
535 maxbibnames=99,
536 isbn=false,doi=false,url=false
537 }
538 % from https://tex.stackexchange.com/a/374059/17418
539 \DeclareCiteCommand{\fullauthorcite}
540 {\usebibmacro{prenote}}
541 {\usedriver
542 {\setcounter{maxnames}{99}% use up to 99 authors
543 \DeclareNameAlias{sortname}{default}}
544 {\thefield{entrytype}}}
545 {\multicitedelim}
546 {\usebibmacro{postnote}}
547 }{}
548 \@ifpackageloaded{csquotes}{%
549 \SetCiteCommand{\footcite}
550 }{}
551 % \end{macrocode}
552 %
553 % And that concludes the |memoir| part.
554 % \begin{macrocode}
555 \fi% end \if@didactic@memoir
556 % \end{macrocode}
557 %
558 % \subsection{A side-caption environment for \texttt{beamer}}
559 %
560 % \begin{environment}{sidecaption}
561 % When using |memoir|, we can use the |sidecaption| environment to put the
562 % caption in the margin.
563 % However, when using |beamer| we don't have this environment, so we need to
564 % provide it with a suitable behaviour.
565 % It's suitable to just translate it to a normal caption, as usually used in
566 % |beamer|.
567 % \begin{macrocode}
568 \ProvideDocumentEnvironment{sidecaption}{omo+b}{%
569 #4
570 \caption{#2\IfValueT{#3}{\label{#3}}}
571 }{\relax}
572 % \end{macrocode}
573 % \end{environment}
574 %
575 % \begin{macro}{\flushscap}
576 % We also want to provide a command to flush a figure towards the caption.
577 % \begin{macrocode}
578 \NewDocumentCommand{\flushscap}{O{\centering}}{%
579 \@ifclassloaded{memoir}{%
580 \ifscapmargleft%
581 \flushleft%
582 \else%
583 \flushright%
584 \fi%
585 }{%
586 #1%
587 }%
588 }
589 % \end{macrocode}
590 % \end{macro}
591 %
592 %
593 % \subsection{More semantic environments}
594 %
595 % We want to provide a set of environments (blocks) for |beamer|.
596 % We want the names of the blocks to be translated.
597 % We'll use the |translations| package for this.
598 % That way we get the language used through the normal use of |babel|.
599 %
600 % We also want to have the same environments for the article mode, but in a
601 % nicer format that the default of |beamerarticle|.
602 % For instance, instead of
603 % \begin{block}{Example}
604 % This is an example.
605 % \end{block}
606 % we want something like
607 % \begin{example}
608 % This is an example.
609 % \end{example}
610 %
611 % This means that we'll need to do one thing if |beamer| is loaded and another
612 % if |beamerarticle| is loaded---or rather, when |beamer| is not loaded, we
613 % should be able to use this without |beamer| and |beamerarticle|.
614 %
615 % We want to provide environments like this one:
616 % \begin{exercise}\label{exercise}
617 % This is an exercise.
618 % \end{exercise}
619 % \begin{exercise}\label{anotherexercise}
620 % This is another exercise.
621 % \end{exercise}
622 % We can use |\cref| to refer to them, getting something like \cref{exercise} or
623 % \cref{exercise,anotherexercise}.
624 %
625 % We also want it to work with different languages, provided there is a
626 % translation.
627 % We do Swedish here.
628 %
629 % \selectlanguage{swedish}
630 % Vi kan också skapa exempel på svenska.
631 % \begin{exercise}\label{exempel}
632 % Detta är ett exempel.
633 % \end{exercise}
634 % \begin{exercise}\label{ytterligareexempel}
635 % Detta är ytterligare ett exempel.
636 % \end{exercise}
637 % Vi kan referera till dem med |\cref|, vilket ger något som \cref{exempel}
638 % eller \cref{exempel,ytterligareexempel}.
639 % \selectlanguage{british}
640 %
641 % Let's also add another idea, but in Swedish.
642 %
643 % \selectlanguage{swedish}
644 % \begin{idea}\label{ide}
645 % Detta är en idé.
646 % \end{idea}
647 % Då har vi \cref{ide}, men vi hade även tidigare \cref{theidea}.
648 % Tillsammans är de \cref{ide,theidea}.
649 % \selectlanguage{british}
650 %
651 % We can also refer to them in English, \cref{ide} and \cref{theidea}
652 % separately and together as \cref{ide,theidea}.
653 %
654 % \begin{macro}{\ProvideSemanticEnv}
655 % We provide a command to create such environments.
656 % This way we just run this command in the preamble, if |beamer| is loaded it
657 % creates the block environments for |beamer|, otherwise it creates the
658 % environments for the article.
659 %
660 % It can be used like this:
661 % \begin{center}
662 % |\ProvideSemanticEnv{test}[alertblock]{Test}[style=definition]|\\
663 % \qquad|{test}{tests}{Test}{Tests}|
664 % \end{center}
665 % \begin{macrocode}
666 \ProvideDocumentCommand{\ProvideSemanticEnv}{m o m o mmmm}{%
667 \@ifundefined{#1}{%
668 \@ifclassloaded{beamer}{% beamer
669 % \end{macrocode}
670 % For |beamer| we want to use the |block| environment, or one of the coloured
671 % blocks that we provide below.
672 % We let the second argument (optional) be the name of the block environment to
673 % use.
674 % This will be the easiest way to set the colour of the block.
675 %
676 % Lastly, we let the third argument be the title of the block.
677 % This is the English title and also the key used to translate the title of the
678 % block.
679 % \begin{macrocode}
680 \IfValueTF{#2}{%
681 \ProvideDocumentEnvironment{#1}{o}{%
682 \begin{#2}{\GetTranslationWarn{#3}\IfValueT{##1}{: ##1}}
683 }{%
684 \end{#2}
685 }
686 }{%
687 \ProvideDocumentEnvironment{#1}{o}{%
688 \begin{block}{\GetTranslationWarn{#3}\IfValueT{##1}{: ##1}}
689 }{%
690 \end{block}
691 }
692 }
693 % \end{macrocode}
694 %
695 % If we don't use |beamer|, we want to use the |thmtools| package to define the
696 % environments.
697 % The fourth argument (optional), will be passed as options to
698 % |\declaretheorem|.
699 % The fifth to eighth arguments are the keys of (meaning English) translations
700 % of the refnames of the block.
701 % \begin{macrocode}
702 }{% not beamer
703 \IfValueTF{#4}{%
704 \declaretheorem[
705 name=\GetTranslationWarn{#3},
706 refname={{\GetTranslationWarn{#5}},{\GetTranslationWarn{#6}}},
707 Refname={{\GetTranslationWarn{#7}},{\GetTranslationWarn{#8}}},
708 #4
709 ]{#1}
710 }{%
711 \declaretheorem[
712 style=definition,
713 name=\GetTranslationWarn{#3},
714 refname={{\GetTranslationWarn{#5}},{\GetTranslationWarn{#6}}},
715 Refname={{\GetTranslationWarn{#7}},{\GetTranslationWarn{#8}}}
716 ]{#1}
717 }
718 }
719 % \end{macrocode}
720 %
721 % Since the |refname| and |Refname| options of |thmtools| doesn't seem to work
722 % we will add the necessary |\crefname| and |\Crefname| commands at the
723 % beginning of the document, for both |beamer| and non-|beamer| case.
724 % \begin{macrocode}
725 \AtBeginDocument{%
726 \@ifpackageloaded{cleveref}{%
727 \crefname{#1}
728 {\GetTranslationWarn{#5}}
729 {\GetTranslationWarn{#6}}
730 \Crefname{#1}
731 {\GetTranslationWarn{#7}}
732 {\GetTranslationWarn{#8}}
733 }{%
734 \relax
735 }
736 }
737 }{\relax} % \@ifundefined{#1}
738 } % \ProvideSemanticEnv
739 % \end{macrocode}
740 % \end{macro}
741 % \begin{environment}{assumption}
742 % \begin{environment}{idea}
743 % \begin{environment}{question}
744 % \begin{environment}{exercise}
745 % \begin{environment}{activity}
746 % \begin{environment}{remark}
747 % \begin{environment}{summary}
748 % Then let's define some useful environments.
749 % \begin{macrocode}
750 \ProvideSemanticEnv{assumption}[alertblock]{Assumption}
751 {assumption}{assumptions}{Assumption}{Assumptions}
752 \ProvideSemanticEnv{idea}[greenblock]{Idea}
753 {idea}{ideas}{Idea}{Ideas}
754 \ProvideSemanticEnv{question}[orangeblock]{Question}
755 {question}{questions}{Question}{Questions}
756 \ProvideSemanticEnv{exercise}[yellowblock]{Exercise}
757 {exercise}{exercises}{Exercise}{Exercises}
758 \ProvideSemanticEnv{activity}[yellowblock]{Activity}
759 {activity}{activities}{Activity}{Activities}
760 \ProvideSemanticEnv{remark}[alertblock]{Remark}[%
761 numbered=no,style=remark
762 ]{remark}{remarks}{Remark}{Remarks}
763 \ProvideSemanticEnv{summary}[block]{Summary}[%
764 numbered=no,style=remark
765 ]{summary}{summaries}{Summary}{Summaries}
766 % \end{macrocode}
767 % \end{environment}
768 % \end{environment}
769 % \end{environment}
770 % \end{environment}
771 % \end{environment}
772 % \end{environment}
773 % \end{environment}
774 %
775 % We also want to provide the normal environments for theorems and definitions
776 % and such.
777 % \begin{macrocode}
778 \if@didactic@theorems
779 \ProvideSemanticEnv{definition}[block]{Definition}
780 {definition}{definitions}{Definition}{Definitions}
781 \ProvideSemanticEnv{theorem}[block]{Theorem}[%
782 numbered=unless unique,style=theorem
783 ]{theorem}{theorems}{Theorem}{Theorems}
784 \ProvideSemanticEnv{corollary}[block]{Corollary}[%
785 numbered=unless unique,style=theorem
786 ]{corollary}{corollaries}{Corollary}{Corollaries}
787 \ProvideSemanticEnv{lemma}[block]{Lemma}[%
788 numbered=unless unique,style=theorem
789 ]{lemma}{lemmas}{Lemma}{Lemmas}
790 \ProvideSemanticEnv{proof}[block]{Proof}[%
791 numbered=no,style=proof
792 ]{proof}{proofs}{Proof}{Proofs}
793 \ProvideSemanticEnv{solution}[block]{Solution}[%
794 numbered=no,style=proof
795 ]{solution}{solutions}{Solution}{Solutions}
796 \ProvideSemanticEnv{example}[exampleblock]{Example}
797 {example}{examples}{Example}{Examples}
798 \fi
799 % \end{macrocode}
800 %
801 %
802 % \subsection{Translations}
803 %
804 % We also want to provide translations for the environments.
805 % We want to provide those even if none of the packages above are loaded.
806 % \begin{macrocode}
807 \ProvideTranslation{swedish}{Assumption}{Antagande}
808 \ProvideTranslation{swedish}{Assumptions}{Antagandena}
809 \ProvideTranslation{swedish}{assumption}{antagande}
810 \ProvideTranslation{swedish}{assumptions}{antagandena}
811 \ProvideTranslation{swedish}{Idea}{Idé}
812 \ProvideTranslation{swedish}{Ideas}{Idéerna}
813 \ProvideTranslation{swedish}{idea}{idé}
814 \ProvideTranslation{swedish}{ideas}{idéerna}
815 \ProvideTranslation{swedish}{Question}{Fråga}
816 \ProvideTranslation{swedish}{Questions}{Frågor}
817 \ProvideTranslation{swedish}{question}{fråga}
818 \ProvideTranslation{swedish}{questions}{frågor}
819 \ProvideTranslation{swedish}{Exercise}{Övningsuppgift}
820 \ProvideTranslation{swedish}{Exercises}{Övningsuppgifterna}
821 \ProvideTranslation{swedish}{exercise}{övningsuppgift}
822 \ProvideTranslation{swedish}{exercises}{övningsuppgifterna}
823 \ProvideTranslation{swedish}{Activity}{Aktivitet}
824 \ProvideTranslation{swedish}{Activities}{Aktiviteter}
825 \ProvideTranslation{swedish}{activity}{aktivitet}
826 \ProvideTranslation{swedish}{activities}{aktiviteter}
827 \ProvideTranslation{swedish}{Note}{Anmärkning}
828 \ProvideTranslation{swedish}{Notes}{Anmärkningar}
829 \ProvideTranslation{swedish}{note}{anmärkning}
830 \ProvideTranslation{swedish}{notes}{anmärkningar}
831 \ProvideTranslation{swedish}{Remark}{Anmärkning}
832 \ProvideTranslation{swedish}{Remarks}{Anmärkningar}
833 \ProvideTranslation{swedish}{remark}{anmärkning}
834 \ProvideTranslation{swedish}{remarks}{anmärkningar}
835 \ProvideTranslation{swedish}{Summary}{Sammanfattning}
836 \ProvideTranslation{swedish}{Summaries}{Sammanfattningar}
837 \ProvideTranslation{swedish}{summary}{sammanfattning}
838 \ProvideTranslation{swedish}{summaries}{sammanfattningar}
839 \ProvideTranslation{swedish}{definition}{definition}
840 \ProvideTranslation{swedish}{definitions}{definitionerna}
841 \ProvideTranslation{swedish}{Definition}{Definition}
842 \ProvideTranslation{swedish}{Definitions}{Definitionerna}
843 \ProvideTranslation{swedish}{theorem}{sats}
844 \ProvideTranslation{swedish}{theorems}{satserna}
845 \ProvideTranslation{swedish}{Theorem}{Sats}
846 \ProvideTranslation{swedish}{Theorems}{Satserna}
847 \ProvideTranslation{swedish}{corollary}{följdsats}
848 \ProvideTranslation{swedish}{Corollary}{Följdsats}
849 \ProvideTranslation{swedish}{corollaries}{följdsatser}
850 \ProvideTranslation{swedish}{Corollaries}{Följdsatser}
851 \ProvideTranslation{swedish}{lemma}{hjälpsats}
852 \ProvideTranslation{swedish}{lemmas}{hjälpsatserna}
853 \ProvideTranslation{swedish}{Lemma}{Hjälpsats}
854 \ProvideTranslation{swedish}{Lemmas}{Hjälpsatserna}
855 \ProvideTranslation{swedish}{proof}{bevis}
856 \ProvideTranslation{swedish}{Proof}{Bevis}
857 \ProvideTranslation{swedish}{proofs}{bevisen}
858 \ProvideTranslation{swedish}{Proofs}{Bevisen}
859 \ProvideTranslation{swedish}{Solution}{Lösningsförslag}
860 \ProvideTranslation{swedish}{Solutions}{Lösningsförslagen}
861 \ProvideTranslation{swedish}{solution}{lösningsförslag}
862 \ProvideTranslation{swedish}{solutions}{lösningsförslagen}
863 \ProvideTranslation{swedish}{Example}{Exempel}
864 \ProvideTranslation{swedish}{Examples}{Exempelen}
865 \ProvideTranslation{swedish}{example}{exempel}
866 \ProvideTranslation{swedish}{examples}{exempelen}
867 % \end{macrocode}
868 %
869 %
870 % \subsection{Coloured blocks}
871 %
872 % \begin{environment}{lightblock}
873 % \begin{environment}{darkblock}
874 % \begin{environment}{blackblock}
875 % \begin{environment}{whiteblock}
876 % \begin{environment}{bwblock}
877 % \begin{environment}{redblock}
878 % \begin{environment}{blueblock}
879 % \begin{environment}{purpleblock}
880 % \begin{environment}{greenblock}
881 % \begin{environment}{yellowblock}
882 % \begin{environment}{orangeblock}
883 % Now we have the coloured blocks.
884 % These we want to define even if |beamer| is not loaded, but for this we need
885 % |beamerarticle| instead.
886 % \begin{macrocode}
887 \ProvideDocumentEnvironment{lightblock}{mm}{%
888 \setbeamercolor{block body}{bg=#1!10,fg=black}
889 \setbeamercolor{block title}{bg=#1,fg=black}
890 \setbeamercolor{local structure}{fg=#1}
891 \begin{block}{#2}
892 }{%
893 \end{block}
894 }
895 \ProvideDocumentEnvironment{darkblock}{mm}{%
896 \setbeamercolor{block body}{bg=#1!10,fg=black}
897 \setbeamercolor{block title}{bg=#1,fg=white}
898 \setbeamercolor{local structure}{fg=#1}
899 \begin{block}{#2}
900 }{%
901 \end{block}
902 }
903 \ProvideDocumentEnvironment{blackblock}{m}
904 {\begin{darkblock}{black}{#1}}
905 {\end{darkblock}}
906 \ProvideDocumentEnvironment{whiteblock}{m}{%
907 \setbeamercolor{block body}{bg=white!10,fg=black}
908 \setbeamercolor{block title}{bg=white,fg=black}
909 \setbeamercolor{local structure}{fg=black}
910 \begin{block}{#1}
911 }{%
912 \end{block}
913 }
914 \ProvideDocumentEnvironment{bwblock}{m}{%
915 \setbeamercolor{block body}{bg=white!10,fg=black}
916 \setbeamercolor{block title}{bg=black,fg=white}
917 \setbeamercolor{local structure}{fg=black}
918 \begin{block}{#1}
919 }{%
920 \end{block}
921 }
922 \ProvideDocumentEnvironment{redblock}{m}
923 {\begin{darkblock}{red}{#1}}
924 {\end{darkblock}}
925 \ProvideDocumentEnvironment{blueblock}{m}
926 {\begin{darkblock}{blue}{#1}}
927 {\end{darkblock}}
928 \ProvideDocumentEnvironment{purpleblock}{m}
929 {\begin{darkblock}{purple}{#1}}
930 {\end{darkblock}}
931 \ProvideDocumentEnvironment{greenblock}{m}
932 {\begin{lightblock}{green}{#1}}
933 {\end{lightblock}}
934 \ProvideDocumentEnvironment{yellowblock}{m}
935 {\begin{lightblock}{yellow}{#1}}
936 {\end{lightblock}}
937 \ProvideDocumentEnvironment{orangeblock}{m}
938 {\begin{lightblock}{orange}{#1}}
939 {\end{lightblock}}
940 % \end{macrocode}
941 % \end{environment}
942 % \end{environment}
943 % \end{environment}
944 % \end{environment}
945 % \end{environment}
946 % \end{environment}
947 % \end{environment}
948 % \end{environment}
949 % \end{environment}
950 % \end{environment}
951 % \end{environment}
952 %
953 % \subsection{Adding margin notes of different kinds}
954 %
955 % We want to add titled margin notes.
956 % These margin notes should use indentation since they might contains several
957 % paragraphs.
958 %
959 % \begin{macro}{\indentedmarginpar}
960 % We start with the indented margin notes.
961 % They simply take a text as argument and typeset it in the margin:
962 % |\indentedmarginpar{this is the text}|.
963 % \indentedmarginpar{this is the text}
964 % \begin{macrocode}
965 % Gives us indentation in the margin notes.
966 % Adapted from https://tex.stackexchange.com/a/257171
967 \NewDocumentCommand{\indentedmarginpar}{+m}{%
968 \@ifclassloaded{memoir}{\strictpagecheck}{\relax}%
969 \marginpar{%
970 \setlength{\parindent}{1.0em}\footnotesize
971 \@afterindentfalse\@afterheading #1
972 }%
973 }
974 % \end{macrocode}
975 % \end{macro}
976 %
977 % \begin{macro}{\newnotetype}
978 % Now, let's add a command to add margin notes with a title.
979 % The |\newnotetype{\titlenote}{Title]| command creates a new command
980 % |\titlenote| that takes a text as argument and typesets it in the margin with
981 % the title |Title|: |\titlenote{this is the text}|.
982 % \newnotetype{\titlenote}{Title}
983 % \titlenote{this is the text}
984 % \begin{macrocode}
985 \NewDocumentCommand{\newnotetype}{omm}{%
986 \@ifclassloaded{beamer}{%
987 \NewDocumentCommand{#2}{+m}{\relax}
988 }{%
989 \IfValueTF{#1}
990 {\NewDocumentCommand{#2}{+m}{%
991 \indentedmarginpar{#1{#3:} ##1}%
992 }}
993 {\NewDocumentCommand{#2}{+m}{%
994 \indentedmarginpar{\emph{#3:} ##1}%
995 }}%
996 }
997 }
998 % \end{macrocode}
999 % \end{macro}
1000 %
1001 % \begin{macro}{\NewNoteType}
1002 % It can also be useful to be able to turn the notes off.
1003 % For instance, we might want notes to appear in one version, but not in
1004 % others.
1005 % Then we'd like a switch to simply turn them off.
1006 % Also, we don't want these notes to appear in |beamer|, so we define them to
1007 % be off by default in that case.
1008 %
1009 % However, to achieve this, we must change the behaviour when creating new
1010 % notes: we need the name, not the |\command| as an argument.
1011 % This is why we have the |\NewNoteType| command.
1012 %
1013 % \ltnote{This is a note with a title, using the note defined by the package.}
1014 % We can use it like this:
1015 % |\NewNoteType{lt}{Learning theory}|
1016 % \NewNoteType{lt}{Learning theory}
1017 % This creates a new note type |\lt|.
1018 % |\lt{This is a note}| yields the note on the side.
1019 % \lt{This is a note}
1020 %
1021 % We can also turn the notes off and on with |\ltoff| and |\lton|.
1022 % \ltoff
1023 % \lt{This is another note while off.}
1024 % \lton
1025 % \lt{This is another note while on, we didn't see the one we used while off.}
1026 % \ltnote{This is a note with a title, using the note defined by the package.}
1027 %
1028 % Let's look at the implementation.
1029 % We first create a new boolean for the note type.
1030 % This will keep track of whether the note type is on or off, should be typeset
1031 % in the margin or if it does nothing.
1032 % As mentioned above, we want to turn the notes off by default in |beamer|.
1033 % \begin{macrocode}
1034 \NewDocumentCommand{\NewNoteType}{omm}{%
1035 \newbool{#2}
1036 \@ifclassloaded{beamer}
1037 {\boolfalse{#2}}
1038 {\booltrue{#2}}
1039 % \end{macrocode}
1040 % Then we create the commands to turn the notes off and on.
1041 % These must be created with |\expandafter\newcommand| to get the name of the
1042 % note type.
1043 % (|\NewDocumentCommand| can't accept names constructed with |\csname|.)
1044 % \begin{macrocode}
1045 \expandafter\newcommand\csname #2off\endcsname{\boolfalse{#2}}
1046 \expandafter\newcommand\csname #2on\endcsname{\booltrue{#2}}
1047 % \end{macrocode}
1048 % We define the command differently depending on whether we want a custom
1049 % formatting for the title of not.
1050 % Same as before, we must use |\expandafter\newcommand| to construct these
1051 % commands.
1052 % \begin{macrocode}
1053 \IfValueTF{#1}{%
1054 \expandafter\newcommand\csname #2\endcsname[1]{%
1055 \ifbool{#2}{%
1056 \indentedmarginpar{#1{#3:} ##1}%
1057 }{\relax}%
1058 }}{%
1059 \expandafter\newcommand\csname #2\endcsname[1]{%
1060 \ifbool{#2}{%
1061 \indentedmarginpar{\emph{#3:} ##1}%
1062 }{\relax}%
1063 }}%
1064 }
1065 % \end{macrocode}
1066 % \end{macro}
1067 %
1068 % \begin{macro}{\ltnote}
1069 % We provide a few different types of margin notes.
1070 % That way we can also include translations for them.
1071 % \begin{macrocode}
1072 \ProvideTranslation{swedish}{Learning theory}{Lärandeteori}
1073 \NewNoteType{ltnote}{\GetTranslationWarn{Learning theory}}
1074 % \end{macrocode}
1075 % \end{macro}
1076 %
1077 %
1078 %
1079 % \subsection{Listing and running code examples}
1080 %
1081 % Let's turn our focus to |\lstexample| and |\runpython|.
1082 % We want to make it easy to typeset and print the output of example Python
1083 % programs in slides and texts.
1084 % We want to use PythonTeX to automatically run the code and typeset the source
1085 % code using |minted|.
1086 %
1087 % These are the same for both |beamer| and article mode.
1088 %
1089 % \begin{macro}{\lstexample}
1090 % The first part is easy.
1091 % To typeset the source file we simply need to use the minted package.
1092 % We provide an optional argument to pass options to minted.
1093 % \begin{macrocode}
1094 \NewDocumentCommand{\lstexample}{omm}{%
1095 \IfValueTF{#1}
1096 {\inputminted[escapeinside=||,#1]{#2}{#3}}
1097 {\inputminted[escapeinside=||]{#2}{#3}}
1098 }
1099 % \end{macrocode}
1100 % \end{macro}
1101 %
1102 % \begin{macro}{\runpython}
1103 % Now, for the second part, printing the output, we do this in two steps.
1104 % First, we create a function using PythonTeX that runs a Python program and
1105 % prints it output.
1106 % We can use it like |\runpython{hello.py}| to run the program in |hello.py|.
1107 % The output is printed in verbatim mode.
1108 % Alternatively we could use |\runpython[opt]{hello.py}| to pass |opt| to the
1109 % underlying |fancyvrb| environment.
1110 %
1111 % Let's create that function in PythonTeX to run the example program file.
1112 % We simply execute it with Python 3 and capture its output.
1113 % Then we print the output to stdout, which is then captured by PythonTeX.
1114 % We use |pytex.add_dependencies| to add the file as a dependency to rerun the
1115 % code when necessary.
1116 % \begin{macrocode}
1117 \begin{pycode}
1118 import subprocess
1119
1120 def run_module(m):
1121 result = subprocess.run(["python3", m], capture_output=True)
1122 print(result.stdout.decode("utf8").strip())
1123 pytex.add_dependencies(m)
1124
1125 def mintedoutput(m, opt):
1126 if opt:
1127 print(r'\begin{minted}[{}]{{text}}'.format(opt))
1128 else:
1129 print(r'\begin{minted}{text}')
1130 run_module(m)
1131 print(r'\end{minted}')
1132 \end{pycode}
1133 % \end{macrocode}
1134 %
1135 % Then we simply create a command that run that function with the file as
1136 % argument.
1137 % Then print the output in verbatim mode.
1138 % \begin{macrocode}
1139 \NewDocumentCommand{\runpython}{om}{%
1140 \setpythontexautoprint{false}%
1141 \pyc{run_module("#2")}%
1142 \printpythontex[verbatim]%
1143 }
1144 % \end{macrocode}
1145 % \end{macro}
1146 %
1147 % \subsection{Contrasting code examples}
1148 %
1149 % Sometimes we want to contrast two code examples side by side.
1150 % \begin{macro}{\textbytext}
1151 % For example |\textbytext{\lipsum[1]}{\lipsum[1]}| should typeset the two
1152 % examples side by side.
1153 % \textbytext{\lipsum[1]}{\lipsum[1]}
1154 % If we use the |memoir| class and our settings (|\if@didactic@memoir|), we
1155 % want to use the |adjustwidth| environment to expand the text block to better
1156 % fit them.
1157 % Otherwise we use the standard text block.
1158 %
1159 % To do this we first add a |fullwidth| environment\footnote{%
1160 % Similarly to \url{https://tex.stackexchange.com/a/350944/17418}.
1161 % }.
1162 % We can get the |adjustwidth| environment from the |changepage| package
1163 % whenever |memoir| isn't used.
1164 % \begin{macrocode}
1165 \RequirePackage{changepage}
1166 \newlength{\@didactic@textbytext@oldcolumnwidth}
1167 \NewDocumentEnvironment{fullwidth}{+b}{%
1168 \setlength{\@didactic@textbytext@oldcolumnwidth}{\columnwidth}
1169 \setlength{\columnwidth}{\textwidth+2em+\marginparwidth+\marginparsep}
1170 \IfStrEqCase{\@didactic@marginparmargin}{%
1171 {inner}{\begin{adjustwidth*}{-\marginparwidth-\marginparsep}{-2em}}
1172 {left}{\begin{adjustwidth}{-\marginparwidth-\marginparsep}{-2em}}
1173 {outer}{\begin{adjustwidth*}{-2em}{-\marginparwidth-\marginparsep}}
1174 {right}{\begin{adjustwidth}{-2em}{-\marginparwidth-\marginparsep}}
1175 }[\relax]
1176 #1
1177 \IfStrEqCase{\@didactic@marginparmargin}{%
1178 {inner}{\end{adjustwidth*}}
1179 {left}{\end{adjustwidth}}
1180 {outer}{\end{adjustwidth*}}
1181 {right}{\end{adjustwidth}}
1182 }[\relax]
1183 \setlength{\columnwidth}{\@didactic@textbytext@oldcolumnwidth}
1184 }{\relax}
1185 % \end{macrocode}
1186 %
1187 % Now to the |\textbytext| command.
1188 % We simply set up a |tabularx| environment with two columns and use it inside
1189 % a |fullwidth| environment.
1190 % \begin{macrocode}
1191 \NewDocumentCommand{\textbytext}{+m+m}{%
1192 \begin{fullwidth}
1193 \begin{tabularx}{\columnwidth}{XX}
1194 #1 & #2
1195 \end{tabularx}
1196 \end{fullwidth}
1197 }
1198 % \end{macrocode}
1199 % \end{macro}
1200 %
1201 % \begin{macro}{\codebycode}
1202 % When we deal with code, we deal with verbatim data.
1203 % The easiest way to deal with this is to simply keep the code in files and
1204 % supply the file names to |\inputminted|.
1205 % The |\codebycode| command is simply two |\inputminted| commands side by side:
1206 % |\codebycode[opt1]{lang1}{file1}[opt2]{lang2}{file2}|.
1207 % \codebycode
1208 % [breaklines,linenos]{python}{hello.py}
1209 % [breaklines,linenos]{python}{hello.py}
1210 %
1211 % This gives us the following implementation.
1212 % We can reuse |\textbytext| to typeset the two examples side by side.
1213 % \begin{macrocode}
1214 \RequirePackage{tabularx}
1215 \RequirePackage{minted}
1216 \NewDocumentCommand{\codebycode}{ommomm}{%
1217 \textbytext{%
1218 \IfValueTF{#1}
1219 {\lstexample[#1]{#2}{#3}}
1220 {\lstexample{#2}{#3}}%
1221 }{%
1222 \IfValueTF{#4}
1223 {\lstexample[#4]{#5}{#6}}
1224 {\lstexample{#5}{#6}}%
1225 }%
1226 }
1227 % \end{macrocode}
1228 % \end{macro}
1229 %
1230 % \begin{macro}{\runbyrun}
1231 % We also want to typeset the runs (|\runpython|).
1232 % We can use |\textbytext| to typeset the two outputs side by side.
1233 % \begin{macrocode}
1234 \NewDocumentCommand{\runbyrun}{mm}{%
1235 \textbytext{\runpython{#1}}{\runpython{#2}}%
1236 }
1237 % \end{macrocode}
1238 % \end{macro}
1239 %
1240 % \Finale
1241 \endinput