-
Notifications
You must be signed in to change notification settings - Fork 0
/
kao.sty
1350 lines (1216 loc) · 47.7 KB
/
kao.sty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\ProvidesPackage{kao}
%----------------------------------------------------------------------------------------
% USEFUL PACKAGES AND COMMANDS
%----------------------------------------------------------------------------------------
\RequirePackage{etoolbox} % Easy programming to modify TeX stuff
\RequirePackage{calc} % Make calculations
\RequirePackage[usenames,dvipsnames,table]{xcolor} % Colours
\RequirePackage{iftex} % Check wether XeTeX is being used
\RequirePackage{xifthen} % Easy conditionals
\RequirePackage{options} % Manage class options
\RequirePackage{xparse} % Parse arguments for macros
\RequirePackage{xpatch} % Patch LaTeX code in external packages
\RequirePackage{xstring} % Parse strings
\RequirePackage{afterpage} % Run commands after specific pages
% \RequirePackage{imakeidx} % For the index; must be loaded before the 'caption' and 'hyperref' packages
\RequirePackage{varioref} % For the cross-references; must be loaded before the 'hyperref' and 'cleveref' packages
% Define \Ifthispageodd (with a capital 'i') to make kaobook compatible with older KOMAScript versions
\@ifpackagelater{scrbase}{2019/12/22}{%
}{%
\let\Ifthispageodd\ifthispageodd%
}
%----------------------------------------------------------------------------------------
% TITLE AND AUTHOR MACROS
%----------------------------------------------------------------------------------------
% Provide an optional argument to the \title command in which to store a plain text title, without any formatting
% Usage: \title[Plain Title]{Actual Title}
\newcommand{\@plaintitle}{}
\renewcommand{\title}[2][]{%
\gdef\@title{#2} % Store the full title in @title
\ifthenelse{\isempty{#1}}{ % If there is no plain title
\renewcommand{\@plaintitle}{\@title} % Use full title
}{ % If there is a plain title
\renewcommand{\@plaintitle}{#1} % Use provided plain-text title
}%
\hypersetup{pdftitle={\@plaintitle}} % Set the PDF metadata title
}
% Provide an optional argument to the \author command in which to store a plain text author, without any formatting
% Usage: \author[Plain Author]{Actual Author}
\newcommand{\@plainauthor}{}
\renewcommand{\author}[2][]{%
\gdef\@author{#2} % Store the full author in @author
\ifthenelse{\isempty{#1}}{ % If there is no plain author
\renewcommand{\@plainauthor}{\@author}% Use full author
}{ % If there is a plain author
\renewcommand{\@plainauthor}{#1}% Use provided plain-text author
}%
\hypersetup{pdfauthor={\@plainauthor}} % Set the PDF metadata author
}
% Make a bookmark to the title page
\pretocmd{\maketitle}{\pdfbookmark[1]{\@plaintitle}{title}}{}{}%
%----------------------------------------------------------------------------------------
% PAGE LAYOUT
%----------------------------------------------------------------------------------------
% Define lengths to set the scale of the document. Changing these
% lengths should affect all the other pagesize-dependent elements in the
% layout, such as the geometry of the page, the spacing between
% paragraphs, and so on. (As of now, not all the elements rely on hscale
% and vscale; future work will address this shortcoming.)
\newlength{\hscale}
\newlength{\vscale}
% By default, the scales are set to work for a4paper
\setlength{\hscale}{1mm}
\setlength{\vscale}{1mm}
\@ifclasswith{\@baseclass}{a0paper}{\setlength{\hscale}{4mm}\setlength{\vscale}{4mm}}{}
\@ifclasswith{\@baseclass}{a1paper}{\setlength{\hscale}{2.828mm}\setlength{\vscale}{2.828mm}}{}
\@ifclasswith{\@baseclass}{a2paper}{\setlength{\hscale}{2mm}\setlength{\vscale}{2mm}}{}
\@ifclasswith{\@baseclass}{a3paper}{\setlength{\hscale}{1.414mm}\setlength{\vscale}{1.414mm}}{}
\@ifclasswith{\@baseclass}{a4paper}{\setlength{\hscale}{1mm}\setlength{\vscale}{1mm}}{}
\@ifclasswith{\@baseclass}{a5paper}{\setlength{\hscale}{0.704mm}\setlength{\vscale}{0.704mm}}{}
\@ifclasswith{\@baseclass}{a6paper}{\setlength{\hscale}{0.5mm}\setlength{\vscale}{0.5mm}}{}
\@ifclasswith{\@baseclass}{b0paper}{\setlength{\hscale}{4.761mm}\setlength{\vscale}{4.761mm}}{}
\@ifclasswith{\@baseclass}{b1paper}{\setlength{\hscale}{3.367mm}\setlength{\vscale}{3.367mm}}{}
\@ifclasswith{\@baseclass}{b2paper}{\setlength{\hscale}{2.380mm}\setlength{\vscale}{2.380mm}}{}
\@ifclasswith{\@baseclass}{b3paper}{\setlength{\hscale}{1.681mm}\setlength{\vscale}{1.681mm}}{}
\@ifclasswith{\@baseclass}{b4paper}{\setlength{\hscale}{1.190mm}\setlength{\vscale}{1.190mm}}{}
\@ifclasswith{\@baseclass}{b5paper}{\setlength{\hscale}{0.837mm}\setlength{\vscale}{0.837mm}}{}
\@ifclasswith{\@baseclass}{b6paper}{\setlength{\hscale}{0.570mm}\setlength{\vscale}{0.570mm}}{}
\@ifclasswith{\@baseclass}{c0paper}{\setlength{\hscale}{4.367mm}\setlength{\vscale}{4.367mm}}{}
\@ifclasswith{\@baseclass}{c1paper}{\setlength{\hscale}{3.085mm}\setlength{\vscale}{3.085mm}}{}
\@ifclasswith{\@baseclass}{c2paper}{\setlength{\hscale}{2.180mm}\setlength{\vscale}{2.180mm}}{}
\@ifclasswith{\@baseclass}{c3paper}{\setlength{\hscale}{1.542mm}\setlength{\vscale}{1.542mm}}{}
\@ifclasswith{\@baseclass}{c4paper}{\setlength{\hscale}{1.090mm}\setlength{\vscale}{1.090mm}}{}
\@ifclasswith{\@baseclass}{c5paper}{\setlength{\hscale}{0.771mm}\setlength{\vscale}{0.771mm}}{}
\@ifclasswith{\@baseclass}{c6paper}{\setlength{\hscale}{0.542mm}\setlength{\vscale}{0.542mm}}{}
\@ifclasswith{\@baseclass}{b0j}{\setlength{\hscale}{4.904mm}\setlength{\vscale}{4.904mm}}{}
\@ifclasswith{\@baseclass}{b1j}{\setlength{\hscale}{3.467mm}\setlength{\vscale}{3.467mm}}{}
\@ifclasswith{\@baseclass}{b2j}{\setlength{\hscale}{2.452mm}\setlength{\vscale}{2.452mm}}{}
\@ifclasswith{\@baseclass}{b3j}{\setlength{\hscale}{1.733mm}\setlength{\vscale}{1.733mm}}{}
\@ifclasswith{\@baseclass}{b4j}{\setlength{\hscale}{1.226mm}\setlength{\vscale}{1.226mm}}{}
\@ifclasswith{\@baseclass}{b5j}{\setlength{\hscale}{0.867mm}\setlength{\vscale}{0.867mm}}{}
\@ifclasswith{\@baseclass}{b6j}{\setlength{\hscale}{0.613mm}\setlength{\vscale}{0.613mm}}{}
\@ifclasswith{\@baseclass}{ansiapaper}{\setlength{\hscale}{1.028mm}\setlength{\vscale}{0.939mm}}{}
\@ifclasswith{\@baseclass}{ansibpaper}{\setlength{\hscale}{1.328mm}\setlength{\vscale}{1.454mm}}{}
\@ifclasswith{\@baseclass}{ansicpaper}{\setlength{\hscale}{2.057mm}\setlength{\vscale}{1.882mm}}{}
\@ifclasswith{\@baseclass}{ansidpaper}{\setlength{\hscale}{2.662mm}\setlength{\vscale}{2.909mm}}{}
\@ifclasswith{\@baseclass}{ansiepaper}{\setlength{\hscale}{4.114mm}\setlength{\vscale}{3.764mm}}{}
\@ifclasswith{\@baseclass}{letterpaper}{\setlength{\hscale}{1.028mm}\setlength{\vscale}{0.939mm}}{}
\@ifclasswith{\@baseclass}{executivepaper}{\setlength{\hscale}{0.876mm}\setlength{\vscale}{0.898mm}}{}
\@ifclasswith{\@baseclass}{legalpaper}{\setlength{\hscale}{1.028mm}\setlength{\vscale}{1.198mm}}{}
\@ifclasswith{\@baseclass}{smallpocketpaper}{\setlength{\hscale}{0.571mm}\setlength{\vscale}{0.639mm}}{}
\@ifclasswith{\@baseclass}{pocketpaper}{\setlength{\hscale}{0.642mm}\setlength{\vscale}{0.723mm}}{}
\@ifclasswith{\@baseclass}{juvenilepaper}{\setlength{\hscale}{0.738mm}\setlength{\vscale}{0.740mm}}{}
\@ifclasswith{\@baseclass}{smallphotopaper}{\setlength{\hscale}{0.809mm}\setlength{\vscale}{0.572mm}}{}
\@ifclasswith{\@baseclass}{photopaper}{\setlength{\hscale}{1.00mm}\setlength{\vscale}{0.707mm}}{}
\@ifclasswith{\@baseclass}{appendixpaper}{\setlength{\hscale}{1.000mm}\setlength{\vscale}{0.505mm}}{}
\@ifclasswith{\@baseclass}{cookpaper}{\setlength{\hscale}{0.809mm}\setlength{\vscale}{0.740mm}}{}
\@ifclasswith{\@baseclass}{illustratedpaper}{\setlength{\hscale}{0.905mm}\setlength{\vscale}{0.909mm}}{}
\@ifclasswith{\@baseclass}{f24paper}{\setlength{\hscale}{0.762mm}\setlength{\vscale}{0.808mm}}{}
\@ifclasswith{\@baseclass}{a4paperlandscape}{\setlength{\hscale}{1.414mm}\setlength{\vscale}{0.707mm}}{}
% Set the default page layout
\RequirePackage[
paperwidth=245\hscale,% 160\hscale,
paperheight=245\vscale,
]{geometry}
% Command to choose among the three possible layouts
\DeclareDocumentCommand{\pagelayout}{m}{%
\ifthenelse{\equal{margin}{#1}}{\marginlayout\marginfloatsetup}{}%
\ifthenelse{\equal{wide}{#1}}{\widelayout\widefloatsetup}{}%
\ifthenelse{\equal{fullwidth}{#1}}{\fullwidthlayout\widefloatsetup}{}%
}
% Layout #1: large margins
\newcommand{\marginlayout}{%
\newgeometry{
top=21\vscale,
bottom=14\vscale,
inner=10\hscale,
textwidth=150\hscale,
marginparsep=8\hscale,
marginparwidth=70\hscale,
}%
\recalchead%
}
% Layout #2: small, symmetric margins
\newcommand{\widelayout}{%
\newgeometry{
top=27.4\vscale,
bottom=27.4\vscale,
inner=24.8\hscale,
outer=24.8\hscale,
marginparsep=0mm,
marginparwidth=0mm,
}%
\recalchead%
}
% Layout #3: no margins and no space above or below the body
\newcommand{\fullwidthpage}{%
\newgeometry{
top=0mm,
bottom=0mm,
inner=0mm,
outer=0mm,
marginparwidth=0mm,
marginparsep=0mm,
}%
\recalchead%
}
% Set the default page layout
\AtBeginDocument{\pagelayout{margin}}
%----------------------------------------------------------------------------------------
% HEADERS AND FOOTERS
%----------------------------------------------------------------------------------------
\RequirePackage{scrlayer-scrpage} % Customise head and foot regions
% Set the header height to prevent a warning
%\setlength{\headheight}{27.4\vscale}
% Increase the space between header and text
\setlength{\headsep}{11\vscale}
% Define some LaTeX lengths used in the page headers
\newlength{\headtextwidth}
\newlength{\headmarginparsep}
\newlength{\headmarginparwidth}
\newlength{\headtotal} % This is the total width of the header
\newcommand{\recalchead}{%
\setlength{\headtextwidth}{\textwidth}%
\setlength{\headmarginparsep}{\marginparsep}%
\setlength{\headmarginparwidth}{\marginparwidth}%
\setlength{\headtotal}{\headtextwidth+\headmarginparsep+\headmarginparwidth}%
}
\AtBeginDocument{% Recalculate the header-related lengths
\recalchead%
}
% Style with chapter number, chapter title, and page in the header (used throughout the document)
\renewpagestyle{scrheadings}{%
{\smash{\hspace{-\headmarginparwidth}\hspace{-\headmarginparsep}\makebox[\headtotal][l]{%
\makebox[7\hscale][r]{\thepage}%
\makebox[3\hscale]{}\rule[-1mm]{0.5pt}{19\vscale-1mm}\makebox[3\hscale]{}%
\makebox[\headtextwidth][l]{\leftmark}}}}%
{}%
{\smash{\makebox[0pt][l]{\makebox[\headtotal][r]{%
\makebox[\headtextwidth][r]{\hfill\rightmark}%
\makebox[3\hscale]{}\rule[-1mm]{0.5pt}{19\vscale-1mm}\makebox[3\hscale]{}%
\makebox[7\hscale][l]{\thepage}}}}}%
}{%
{}%
{}%
{}%
}
% Style with neither header nor footer (used for special pages)
\renewpagestyle{plain.scrheadings}{%
{}%
{}%
{}%
}{%
{}%
{}%
{}%
}
% Style with an empty header and the page number in the footer
\newpagestyle{pagenum.scrheadings}{%
{}%
{}%
{}%
}{%
{\makebox[\textwidth][r]{\thepage}}%
{\makebox[\textwidth][l]{\thepage}}%
{\makebox[\textwidth][l]{\thepage}}%
}
% Style with an empty header and the page number in the footer
\newpagestyle{centeredpagenum.scrheadings}{%
{}%
{}%
{}%
}{%
{\hspace{-\headmarginparwidth}\hspace{-\headmarginparsep}\makebox[\headtotal][l]{\hfill\thepage\hfill}}
{\makebox[0pt][l]{\makebox[\headtotal][r]{\hfill\thepage\hfill}}}%
{\makebox[0pt][l]{\makebox[\headtotal][r]{\hfill\thepage\hfill}}}%
}
% Command to print a blank page
\newcommand\blankpage{%
\null%
\thispagestyle{empty}%
\newpage%
}
% Set the default page style
\pagestyle{scrheadings}
%----------------------------------------------------------------------------------------
% PARAGRAPH FORMATTING
%----------------------------------------------------------------------------------------
\RequirePackage{ragged2e} % Required to achieve better ragged paragraphs
\RequirePackage{setspace} % Required to easily set the space between lines
\RequirePackage{hyphenat} % Hyphenation for special fonts
\RequirePackage{microtype} % Improves character and word spacing
\RequirePackage{needspace} % Required to prevent page break right after a sectioning command
\RequirePackage{xspace} % Better print trailing whitespace
% TODO: recognize space/indent justified/raggedright class options
% Settings for a normal paragraph
\newcommand{\@body@par}{%
\justifying% Justify text
\singlespacing% Set the interline space to a single line
\frenchspacing% No additional space after periods
\normalfont% Use the default font
\normalsize% Use the default size
}
% Settings for paragraphs in the margins
\newcommand{\@margin@par}{%
\justifying% justify text
\setlength{\RaggedRightParindent}{0em}% Suppress indentation
\setlength{\parindent}{0em}% Suppress indentation
\setlength{\parskip}{0.5pc}% Set the space between paragraphs
%\singlespacing% Set the space between lines
\frenchspacing% No additional space after periods
\normalfont% Use the default font
\footnotesize% Use a smaller size
}
% By default, use @body@par settings
\@body@par
%----------------------------------------------------------------------------------------
% WIDE PARAGRAPHS
%----------------------------------------------------------------------------------------
% Environment for a wide paragraph
\NewDocumentEnvironment{widepar}{}{%
\if@twoside%
\Ifthispageodd{%
\begin{addmargin}[0cm]{-\marginparwidth-\marginparsep}%
}{%
\begin{addmargin}[-\marginparwidth-\marginparsep]{0cm}%
}%
\else%
\begin{addmargin}[0cm]{-\marginparwidth-\marginparsep}%
\fi%
}{%
\end{addmargin}%
}
% Environment for a full width paragraph
\NewDocumentEnvironment{fullwidthpar}{}{%
\if@twoside%
\Ifthispageodd{%
\begin{addmargin}[-1in-\hoffset-\oddsidemargin]{-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth}%
}{%
\begin{addmargin}[-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth]{-\paperwidth+1in+\hoffset+\oddsidemargin+\marginparsep+\marginparwidth+\textwidth}%
}%
\else%
\begin{addmargin}[-1in-\hoffset-\oddsidemargin]{-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth}%
\fi%
}{%
\end{addmargin}%
}
% Environment for a wide equation
\NewDocumentEnvironment{wideequation}{}{%
\begin{widepar}%
\begin{equation}%
}{%
\end{equation}%
\end{widepar}%
}
%----------------------------------------------------------------------------------------
% FOOTNOTES, MARGINNOTES AND SIDENOTES
%----------------------------------------------------------------------------------------
\RequirePackage[section]{placeins} % Prevent floats to cross sections
\extrafloats{100} % Require more floats
\RequirePackage{marginnote} % Provides options for margin notes
\RequirePackage{marginfix} % Make marginpars float freely
\RequirePackage{sidenotes} % Use sidenotes
\RequirePackage{chngcntr} % Reset counters at sections
% TODO: see also page 440 of the KOMA-script guide
\RequirePackage[
bottom,
symbol*,
hang,
flushmargin,
% perpage,
stable,
]{footmisc} % Required to set up the footnotes
\RequirePackage{footnotebackref} % Creates back references from footnotes to text
% Fix the color of the footnote marker when back-referenced
\patchcmd{\footref}{\ref}{\hypersetup{colorlinks=black}\ref}{}{}%
% Workaround to fix back references
\edef\BackrefFootnoteTag{bhfn:\theBackrefHyperFootnoteCounter}%
% FIXME: I am not able to choose the paragraph layout of footnotes, probably the footnotes package conflicts with scrbook.
%\renewcommand{\footnotelayout}{\@margin@par}
%----------------------------------------------------------------------------------------
% Justify and format margin notes
\renewcommand*{\raggedleftmarginnote}{} % Suppress left margin
\renewcommand*{\raggedrightmarginnote}{} % Suppress right margin
\renewcommand*{\marginfont}{\@margin@par} % Format marginnotes according to \@marginpar (see above)
\renewcommand{\marginnotevadjust}{0.8\vscale} % Bring all marginnotes downwards a bit
\marginposadjustment=0.1mm % Bring downwards also the marginpars
\marginheightadjustment=.5cm % Bring downwards also the marginpars
%\RequirePackage[marginnote=true]{snotez} % Provides options for sidenotes
% Copied from snotez's \sidenote
\def\kao@if@nblskip#1{%
\expandafter\ifx\@car#1\@nil*%
\expandafter\@firstoftwo%
\else%
\expandafter\@secondoftwo%
\fi%
}
% Command to detect whether we are inside an mdframed environment
\newif\ifinfloat
\AtBeginEnvironment{mdframed}{\infloattrue}
\AtBeginEnvironment{minipage}{\infloattrue}
\def\IfInFloatingEnvir{%
\ifinfloat%
\expandafter\@firstoftwo%
\else%
\expandafter\@secondoftwo%
\fi%
}
% Redefine the command to print marginnotes:
% (a) the optional offset argument goes at the first position
% (b) the offset can also be a multiple of baselineskip, like for snotez's \sidenote
% Usage: \marginnote[<offset>]{Text of the note.}
\let\oldmarginnote\marginnote%
\RenewDocumentCommand\marginnote{ o m }{%
\IfNoValueOrEmptyTF{#1}{%
\IfInFloatingEnvir{%
\oldmarginnote{#2}%
}{%
\marginpar{\@margin@par#2}%
}%
}{%
\oldmarginnote{#2}[\kao@if@nblskip{#1}{\@cdr#1\@nil\baselineskip}{#1}]%
}%
}
% Initially set the counter to zero instead of 1, and update it before printing the sidenote.
\setcounter{sidenote}{0}%
\RenewDocumentCommand\sidenote{ o o +m }{%
\IfNoValueOrEmptyTF{#1}{%
\refstepcounter{sidenote}% This command has been moved here
}{%
}%
\sidenotemark[#1]%
\sidenotetext[#1][#2]{#3}%
\@sidenotes@multimarker%
}
% % Formatting sidenotes
% \setsidenotes{
% text-mark-format=\textsuperscript{\normalfont#1}, % Use a superscript for the marker in the text
% note-mark-format=#1:, % Use a normal font for the marker in the margin; use a colon after the sidenote number
% note-mark-sep=\enskip, % Set the space after the marker
% }
% Formatting sidenotes
\RenewDocumentCommand\@sidenotes@thesidenotemark{ m }{%
\leavevmode%
\ifhmode%
\edef\@x@sf{\the\spacefactor}%
\nobreak%
\fi%
\hbox{\@textsuperscript{\normalfont#1}}%
\ifhmode%
\spacefactor\@x@sf%
\fi%
\relax%
}%
\RenewDocumentCommand\sidenotetext{ o o +m }{% number, offset, text
\IfNoValueOrEmptyTF{#1}{%
\marginnote[#2]{\thesidenote:\enskip#3}%
}{%
\marginnote[#2]{#1:\enskip#3}%
}%
}
%----------------------------------------------------------------------------------------
% FIGURES, TABLES, LISTINGS AND CAPTIONS
%----------------------------------------------------------------------------------------
\RequirePackage{graphicx} % Include figures
\setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio} % Improves figure scaling
\RequirePackage{tikz} % Allows to draw custom shapes
\RequirePackage{tikzpagenodes} % Allows to anchor tikz nodes to page elements
\RequirePackage{booktabs} % Nicer tables
\RequirePackage{multirow} % Cells occupying multiple rows in tables
\RequirePackage{multicol} % Multiple columns in dictionary
\RequirePackage{rotating} % Allows tables and figures to be rotated
\RequirePackage{listings} % Print code listings
%\RequirePackage{minted}
\RequirePackage[hypcap=true]{caption} % Correctly placed anchors for hyperlinks
% \RequirePackage{atbegshi}
% \RequirePackage{perpage}
\let\c@abspage\relax
% \newcommand{\pp@g@sidenote}{}
\RequirePackage{floatrow} % Set up captions of floats
%\RequirePackage{dblfloatfix} % Better positioning of wide figures and tables
\AtEndPreamble{\RequirePackage{scrhack}} % Make packages compatible with KOMA Script (must be loaded last: https://tex.stackexchange.com/questions/156240/latex-packages-minted-and-scrhack)
% Improve the figure placing (see https://www.overleaf.com/learn/latex/Tips)
\def\topfraction{.9}%
\def\textfraction{0.35}%
\def\floatpagefraction{0.8}%
% Set the space between floats and main text
\renewcommand\FBaskip{.4\topskip}%
\renewcommand\FBbskip{\FBaskip}%
% Tighten up space between displays (e.g., equations) and make symmetric (from tufte-latex)
\setlength\abovedisplayskip{6pt plus 2pt minus 4pt}%
\setlength\belowdisplayskip{6pt plus 2pt minus 4pt}%
\abovedisplayskip 10\p@ \@plus2\p@ \@minus5\p@%
\abovedisplayshortskip \z@ \@plus3\p@%
\belowdisplayskip \abovedisplayskip%
\belowdisplayshortskip 6\p@ \@plus3\p@ \@minus3\p@%
\setlength\columnseprule{.4pt} % Set the width of vertical rules in tables
\newlength{\kaomarginskipabove} % Specify the space above a marginfigure, margintable or marginlisting
\newlength{\kaomarginskipbelow} % Specify the space below a marginfigure, margintable or marginlisting
\setlength{\kaomarginskipabove}{3mm plus 2pt minus 2pt}
\setlength{\kaomarginskipbelow}{3mm plus 2pt minus 2pt}
% Environment to hold a margin figure (from the sidenotes package)
% We redefine it here because we want to use our own caption formatting.
\RenewDocumentEnvironment{marginfigure}{o}{%
\FloatBarrier%
\marginskip{\kaomarginskipabove}%
\begin{lrbox}{\@sidenotes@marginfigurebox}%
\begin{minipage}{\marginparwidth}%
\captionsetup{type=figure}%
}{%
\end{minipage}%
\end{lrbox}%
\marginnote[#1]{\usebox{\@sidenotes@marginfigurebox}}%
\marginskip{\kaomarginskipbelow}%
}
% Environment to hold a margin table (from the sidenotes package)
\RenewDocumentEnvironment{margintable}{o}{%
\FloatBarrier%
\marginskip{\kaomarginskipabove}%
\begin{lrbox}{\@sidenotes@margintablebox}%
\begin{minipage}{\marginparwidth}%
\captionsetup{type=table}%
}{%
\end{minipage}%
\end{lrbox}%
\marginnote[#1]{\usebox{\@sidenotes@margintablebox}}%
\marginskip{\kaomarginskipbelow}%
}
% Environment to hold a margin listing
\newsavebox{\@sidenotes@marginlistingbox}%
\NewDocumentEnvironment{marginlisting}{o}{% The optional parameter is the vertical offset
\FloatBarrier%
\marginskip{\kaomarginskipabove}%
\begin{lrbox}{\@sidenotes@marginlistingbox}%
\begin{minipage}{\marginparwidth}%
\captionsetup{type=lstlisting}%
}{%
\end{minipage}%
\end{lrbox}%
\marginnote[#1]{\usebox{\@sidenotes@marginlistingbox}}%
\marginskip{\kaomarginskipbelow}%
}
% Change the position of the captions
\DeclareFloatSeparators{marginparsep}{\hskip\marginparsep}%
% Detect whether there is a caption in the current environment by switching the kaocaption toggle when \caption is called. If there is no caption, reset the floatsetup. Without this fix, the floatrow package will align the environment to the main text if there is a caption, but to the margin if there is no caption.
\newtoggle{kaocaption}
\AtBeginEnvironment{figure}{%
\let\oldcaption\caption%
\RenewDocumentCommand{\caption}{s o m}{%
\IfBooleanTF{#1}{%
\oldcaption*{#3}%
}{%
\IfValueTF{#2}{%
\oldcaption[#2]{#3}%
}{%
\oldcaption{#3}%
}%
}%
\toggletrue{kaocaption}%
}%
}
\AtEndEnvironment{figure}{%
\iftoggle{kaocaption}{%
}{%
\RawFloats%
\centering%
}%
\togglefalse{kaocaption}%
}
\AtBeginEnvironment{table}{%
\let\oldcaption\caption%
\RenewDocumentCommand{\caption}{s o m}{%
\IfBooleanTF{#1}{%
\oldcaption*{#3}%
}{%
\IfValueTF{#2}{%
\oldcaption[#2]{#3}%
}{%
\oldcaption{#3}%
}%
}%
\toggletrue{kaocaption}%
}%
}
\AtEndEnvironment{table}{%
\iftoggle{kaocaption}{%
}{%
\RawFloats%
\centering%
}%
\togglefalse{kaocaption}%
}
% Change the formatting of the captions
\addtokomafont{captionlabel}{\bfseries} % Bold font for the figure label
% Declare a new style to format the caption according to \@margin@par (see above)
\DeclareCaptionFormat{margin}{\@margin@par #1#2#3}
% Declare a new caption style for lstlistings
\newsavebox\mycap
\DeclareCaptionFormat{llap}{%
\begin{lrbox}{\mycap}%
\begin{minipage}{\marginparwidth}%
\@margin@par #1:#2#3%
\end{minipage}%
\end{lrbox}%
\marginnote[0.2cm]{\usebox\mycap}%
}
% Set the global caption style
\captionsetup{
format=margin, % Use the style previously declared
strut=no,%
%hypcap=true, % Links point to the top of the figure
singlelinecheck=false,%
%width=\marginparwidth,
indention=0pt, % Suppress indentation
parindent=0pt, % Suppress space between paragraphs
aboveskip=6pt, % Increase the space between the figure and the caption
belowskip=6pt, % Increase the space between the caption and the table
}
% Needed to have continued figures and tables (https://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions#Figures_in_multiple_parts)
\DeclareCaptionLabelFormat{cont}{#1~#2\alph{ContinuedFloat}}
\captionsetup[ContinuedFloat]{labelformat=cont}
% Captions for the 'margin' layout
\NewDocumentCommand{\marginfloatsetup}{}{%
\if@twoside%
\floatsetup[figure]{% Captions for figures
margins=hangoutside,% Put captions in the margins
facing=yes,%
capposition=beside,%
capbesideposition={bottom,outside},%
capbesideframe=yes,%
capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
capbesidesep=marginparsep,%
floatwidth=\textwidth,% Width of the figure equal to the width of the text
}%
\floatsetup[widefigure]{% Captions for wide figures
margins=hangoutside,% Put captions below the figure
facing=yes,%
capposition=bottom%
}%
\floatsetup[table]{% Captions for tables
margins=hangoutside,% Put captions in the margin
facing=yes,%
capposition=beside,%
capbesideposition={top,outside},%
%capbesideposition=outside,
capbesideframe=yes,%
capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
capbesidesep=marginparsep,%
floatwidth=\textwidth,% Width of the figure equal to the width of the text
}%
\floatsetup[widetable]{% Captions for wide tables
margins=hangoutside,% Put captions above the table
facing=yes,%
capposition=above%
}%
\floatsetup[longtable]{% Captions for longtables
margins=raggedright,% Overwrite the hangright setting from the `table' environment
%LTcapwidth=table,% Set the width of the caption equal to the table's
}%
\floatsetup[lstlisting]{% Captions for lstlistings
margins=hangoutside,% Put captions in the margin
facing=yes,%
capposition=beside,%
capbesideposition={top,outside},%
%capbesideposition=outside,
capbesideframe=yes,%
capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
capbesidesep=marginparsep,%
floatwidth=\textwidth,% Width of the figure equal to the width of the text
}%
\floatsetup[listing]{% Captions for listings (minted package)
margins=hangoutside,% Put captions in the margin
facing=yes,%
capposition=beside,%
capbesideposition={top,outside},%
%capbesideposition=outside,
capbesideframe=yes,%
capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
capbesidesep=marginparsep,%
floatwidth=\textwidth,%Width of the figure equal to the width of the text
}%
\captionsetup*[lstlisting]{%
format=llap,%
labelsep=space,%
singlelinecheck=no,%
belowskip=-0.6cm,%
}%
\else%
\floatsetup[figure]{% Captions for figures
margins=hangright,% Put captions in the margins
facing=yes,%
capposition=beside,%
capbesideposition={bottom,right},%
capbesideframe=yes,%
capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
capbesidesep=marginparsep,%
floatwidth=\textwidth,% Width of the figure equal to the width of the text
}%
\floatsetup[widefigure]{% Captions for wide figures
margins=hangright,% Put captions below the figure
facing=no,%
capposition=bottom%
}%
\floatsetup[table]{% Captions for tables
margins=hangright,% Put captions in the margin
facing=yes,%
capposition=beside,%
capbesideposition={top,right},%
%capbesideposition=outside,
capbesideframe=yes,%
capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
capbesidesep=marginparsep,%
floatwidth=\textwidth,% Width of the figure equal to the width of the text
}%
\floatsetup[widetable]{% Captions for wide tables
margins=hangright,% Put captions above the table
facing=no,%
capposition=above%
}%
\floatsetup[longtable]{% Captions for longtables
margins=raggedright,% Overwrite the hangright setting from the `table' environment
%LTcapwidth=table,% Set the width of the caption equal to the table's
}%
\floatsetup[lstlisting]{% Captions for lstlisting
margins=hangright,% Put captions in the margin
facing=yes,%
capposition=beside,%
capbesideposition={top,right},%
%capbesideposition=outside,
capbesideframe=yes,%
capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
capbesidesep=marginparsep,%
floatwidth=\textwidth,% Width of the figure equal to the width of the text
}%
\floatsetup[listing]{% Captions for listing (minted package)
margins=hangright,% Put captions in the margin
facing=yes,%
capposition=beside,%
capbesideposition={top,right},%
%capbesideposition=outside,
capbesideframe=yes,%
capbesidewidth=\marginparwidth,% Width of the caption equal to the width of the margin
capbesidesep=marginparsep,%
floatwidth=\textwidth,% Width of the figure equal to the width of the text
}%
\captionsetup*[lstlisting]{%
format=llap,%
labelsep=space,%
singlelinecheck=no,%
belowskip=-0.6cm,%
}%
\fi%
}
% Captions for the 'wide' layout
\NewDocumentCommand{\widefloatsetup}{}{%
\floatsetup[figure]{ % Captions for figures
capposition=bottom,%
margins=centering,%
floatwidth=\textwidth%
}
\floatsetup[widefigure]{ % Captions for wide figures
margins=hangoutside, % Put captions below the figure
facing=yes,%
capposition=bottom%
}
\floatsetup[table]{ % Captions for tables
capposition=above,%
margins=centering,%
floatwidth=\textwidth%
}
\floatsetup[widetable]{ % Captions for wide tables
margins=hangoutside, % Put captions above the table
facing=yes,%
capposition=above%
}
\floatsetup[lstlisting]{ % Captions for lstlistings
capposition=above,%
margins=centering,%
floatwidth=\textwidth%
}
\floatsetup[listing]{ % Captions for listings (minted package)
capposition=above,%
margins=centering,%
floatwidth=\textwidth%
}
\captionsetup*[lstlisting]{% Captions style for lstlistings
%format=margin,%
labelsep=colon,%
strut=no,%
singlelinecheck=false,%
indention=0pt,%
parindent=0pt,%
aboveskip=6pt,%
belowskip=6pt,%
belowskip=-0.1cm%
}
}
%----------------------------------------------------------------------------------------
% TOC, LOF & LOT
%----------------------------------------------------------------------------------------
\RequirePackage{tocbasic}
% Show an entry for the TOC in the TOC
\setuptoc{toc}{totoc}
% Choose the levels in table of contents
\setcounter{tocdepth}{\subsectiontocdepth}
% Define the style for toc entries
\@ifpackagelater{scrbase}{2019/10/11}{%
\DeclareTOCStyleEntries[indent=0em,numwidth=2em,dynnumwidth=yes,pagenumberwidth=2.1em]{tocline}{figure,table}%
\DeclareTOCStyleEntries[dynnumwidth=yes]{tocline}{subsubsection,subsection,section,chapter,part}%
\DeclareTOCStyleEntries[pagenumberwidth=2.5em]{tocline}{chapter,part}%
\DeclareTOCStyleEntries[pagenumberwidth=2.1em]{tocline}{subsubsection,subsection,section}%
}{%
\DeclareTOCStyleEntries[indent=0em,numwidth=2em,dynnumwidth=yes]{tocline}{figure,table}%
\DeclareTOCStyleEntries[dynnumwidth=yes]{tocline}{subsubsection,subsection,section,chapter,part}%
}
% Define the names for the headings
% \renewcaptionname{english}{\contentsname}{Detailed Contents}
% \renewcaptionname{english}{\listfigurename}{Figures}
% \renewcaptionname{english}{\listtablename}{Tables}
% \newcaptionname{english}{\listtheoremname}{Theorems}
%----------------------------------------------------------------------------------------
% MARGIN TOC
%----------------------------------------------------------------------------------------
\RequirePackage{etoc} % Required to insert local tables of contents
\newcounter{margintocdepth} % Set the depth of the margintoc
\setcounter{margintocdepth}{\sectiontocdepth}
\newlength{\mtocshift} % Length of the vertical offset used for margin tocs
\setlength{\mtocshift}{1\vscale}% \mtocshift is overridden by \setchapterstyle
% Optional title for margintoc (by hCarlFelix,
% https://github.com/fmarotta/kaobook/issues/101)
% We want to create an additional entries in the toc which is to be used for the margintoc
% We define these as mtocsection and mtocsubsection for section and subsection`
\newcommand{\mtocsection}[1]{
\addcontentsline{toc}{mtocsection}{%
\ifnum\value{secnumdepth}>0 \protect\numberline{\thesection}%
\else \protect\nonumberline%
\fi #1}%
}
\newcommand{\mtocsubsection}[1]{
\addcontentsline{toc}{mtocsubsection}{%
\ifnum\value{secnumdepth}>1 \protect\numberline{\thesubsection}%
\else \protect\nonumberline%
\fi #1}%
}
% Next, we redefine \section and \subsection so that they accept an additional parameter
% \section[alternative-title-for-toc]{title-as-written-in-text}[alternative-title-for-margintoc]
% Adapted from Frank Mittelbach's answer at Stackexchange
% https://tex.stackexchange.com/a/109560/226693
\let\oldsection\section % save the old command
\let\oldsubsection\subsection % save the old command
\RenewDocumentCommand\section{s o m o}{%
\IfBooleanTF{#1}{%
\oldsection*{#3}
\IfNoValueF{#2}{% if TOC arg is given create a TOC entry
\addxcontentsline{toc}{section}[\thesection]{#2}%
}%
}{% no star given
\IfNoValueTF{#2}{%
\oldsection{#3}%
}{% no TOC arg
\oldsection[#2]{#3}%
}%
\IfNoValueTF{#4}{% optional label given, if not we do nothing
\mtocsection{#3}%
}{%
\mtocsection{#4}%
}%
}%
}
\RenewDocumentCommand\subsection{s o m o}{
\IfBooleanTF{#1}{%
\oldsubsection*{#3}%
\IfNoValueF{#2}{% if TOC arg is given create a TOC entry
\addxcontentsline{toc}{subsection}[\thesubsection]{#2}%
}%
}{% no star given
\IfNoValueTF{#2}{%
\oldsubsection{#3}%
}{% no TOC arg
\oldsubsection[#2]{#3}%
}%
\IfNoValueTF{#4}{% optional label given, if not we do nothing
\mtocsubsection{#3}%
}{%
\mtocsubsection{#4}%
}%
}%
}
\etocsetlevel{mtocsection}{6}% dummy sectioning level
\etocsetlevel{mtocsubsection}{6}% dummy sectioning level
% Command to print a table of contents in the margin
\NewDocumentCommand{\margintoc}{O{\mtocshift}}{ % The first parameter is the vertical offset; by default it is \mtocshift
\begingroup%
% Move regular section and subsection to level 6 so that they won't be included and instead set let the mtoc versions take their place.
% Adapted from https://tex.stackexchange.com/a/133559/226693
\etocsetlevel{section}{6}
\etocsetlevel{subsection}{6}
\etocsetlevel{mtocsection}{1}
\etocsetlevel{mtocsubsection}{2}
% Define default widths
\def\margintocnumwidth{-.8mm}%
\def\margintocpagenumwidth{8pt}%
\setlength{\RaggedRightParfillskip}{0pt}
% Dry run to get the needed widths
\etocsetstyle{mtocsection}%
{}%
{\setbox0\hbox{\usekomafont{section}\small\etocthenumber\kern.8mm}%%
\setbox1\hbox{\usekomafont{section}\small\etocthepage}}%
{\ifdim\wd0>\margintocnumwidth \edef\margintocnumwidth{\the\wd0} \fi%%
\ifdim\wd1>\margintocpagenumwidth \edef\margintocpagenumwidth{\the\wd1} \fi}%
{}%
\etocsetstyle{mtocsubsection}%
{}%
{\setbox0\hbox{\usekomafont{section}\small\etocthenumber\kern.8mm}%
\setbox1\hbox{\usekomafont{section}\small\etocthepage}}%
{\ifdim\wd0>\margintocnumwidth \edef\margintocnumwidth{\the\wd0} \fi%
\ifdim\wd1>\margintocpagenumwidth \edef\margintocpagenumwidth{\the\wd1} \fi}%
{}%
\etocsetstyle{subsubsection}%
{}%
{}%
{}%
{}%
\etocsetstyle{paragraph}%
{}%
{}%
{}%
{}%
\etocsettocstyle{}{%
\global\let\margintocnumwidth\margintocnumwidth%
\global\let\margintocpagenumwidth\margintocpagenumwidth%
}%
\localtableofcontents%
% Set the style for section entries
\etocsetstyle{mtocsection}%
{\parindent 0pt \parskip 2.5pt \parfillskip 0pt \RaggedRight}%
{\leftskip \margintocnumwidth \rightskip \margintocpagenumwidth} %
{\makebox[0pt][r]{\makebox[\margintocnumwidth][l]{\etocnumber}}\etocname\nobreak\leaders\hbox{\hbox to 1.5ex {\hss.\hss}}\hfill\rlap{\makebox[\margintocpagenumwidth][r]{\etocpage}}\par}%
{}%
% Set the style for subsection entries
\etocsetstyle{mtocsubsection}%
{\parindent 0pt \parskip 0pt \parfillskip 0pt \RaggedRight}%
{\leftskip \margintocnumwidth \rightskip \margintocpagenumwidth}%
{\makebox[0pt][r]{\makebox[\margintocnumwidth][l]{\etocnumber}}\etocname\nobreak\leaders\hbox{\hbox to 1.5ex {\hss.\hss}}\hfill\rlap{\makebox[\margintocpagenumwidth][r]{\etocpage}}\par}%
{\parskip 2.5pt}%
% Set the global style of the toc
\etocsettocstyle{\usekomafont{section}\small}{}%
\etocsetnexttocdepth{\themargintocdepth}%
% Print the table of contents in the margin
\marginnote[#1]{\localtableofcontents}%
\FloatBarrier%
\endgroup%
}
%----------------------------------------------------------------------------------------
% ENCODING AND FONTS
%----------------------------------------------------------------------------------------
% https://tex.stackexchange.com/questions/47576/combining-ifxetex-and-ifluatex-with-the-logical-or-operation
% Introduce a command to find out whether the compiler is XeTeX or LuaTeX