kerneldoc-preamble.sty 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. % -*- coding: utf-8 -*-
  2. % SPDX-License-Identifier: GPL-2.0
  3. %
  4. % LaTeX preamble for "make latexdocs" or "make pdfdocs" including:
  5. % - TOC width settings
  6. % - Setting of tabulary (\tymin)
  7. % - Headheight setting for fancyhdr
  8. % - Fontfamily settings for CJK (Chinese, Japanese, and Korean) translations
  9. %
  10. % Note on the suffix of .sty:
  11. % This is not implemented as a LaTeX style file, but as a file containing
  12. % plain LaTeX code to be included into preamble.
  13. % ".sty" is chosen because ".tex" would cause the build scripts to confuse
  14. % this file with a LaTeX main file.
  15. %
  16. % Copyright (C) 2022 Akira Yokosawa
  17. % Custom width parameters for TOC
  18. % - Redefine low-level commands defined in report.cls.
  19. % - Indent of 2 chars is preserved for ease of comparison.
  20. % Summary of changes from default params:
  21. % Width of page number (\@pnumwidth): 1.55em -> 2.7em
  22. % Width of chapter number: 1.5em -> 2.4em
  23. % Indent of section number: 1.5em -> 2.4em
  24. % Width of section number: 2.6em -> 3.2em
  25. % Indent of subsection number: 4.1em -> 5.6em
  26. % Width of subsection number: 3.5em -> 4.3em
  27. %
  28. % These params can have 4 digit page counts, 3 digit chapter counts,
  29. % section counts of 4 digits + 1 period (e.g., 18.10), and subsection counts
  30. % of 5 digits + 2 periods (e.g., 18.7.13).
  31. \makeatletter
  32. %% Redefine \@pnumwidth (page number width)
  33. \renewcommand*\@pnumwidth{2.7em}
  34. %% Redefine \l@chapter (chapter list entry)
  35. \renewcommand*\l@chapter[2]{%
  36. \ifnum \c@tocdepth >\m@ne
  37. \addpenalty{-\@highpenalty}%
  38. \vskip 1.0em \@plus\p@
  39. \setlength\@tempdima{2.4em}%
  40. \begingroup
  41. \parindent \z@ \rightskip \@pnumwidth
  42. \parfillskip -\@pnumwidth
  43. \leavevmode \bfseries
  44. \advance\leftskip\@tempdima
  45. \hskip -\leftskip
  46. #1\nobreak\hfil
  47. \nobreak\hb@xt@\@pnumwidth{\hss #2%
  48. \kern-\p@\kern\p@}\par
  49. \penalty\@highpenalty
  50. \endgroup
  51. \fi}
  52. %% Redefine \l@section and \l@subsection
  53. \renewcommand*\l@section{\@dottedtocline{1}{2.4em}{3.2em}}
  54. \renewcommand*\l@subsection{\@dottedtocline{2}{5.6em}{4.3em}}
  55. \makeatother
  56. %% Prevent default \sphinxtableofcontentshook from overwriting above tweaks.
  57. \renewcommand{\sphinxtableofcontentshook}{} % Empty the hook
  58. % Prevent column squeezing of tabulary. \tymin is set by Sphinx as:
  59. % \setlength{\tymin}{3\fontcharwd\font`0 }
  60. % , which is too short.
  61. \setlength{\tymin}{20em}
  62. % Adjust \headheight for fancyhdr
  63. \addtolength{\headheight}{1.6pt}
  64. \addtolength{\topmargin}{-1.6pt}
  65. % Translations have Asian (CJK) characters which are only displayed if
  66. % xeCJK is used
  67. \usepackage{ifthen}
  68. \newboolean{enablecjk}
  69. \setboolean{enablecjk}{false}
  70. \IfFontExistsTF{Noto Sans CJK SC}{
  71. \IfFileExists{xeCJK.sty}{
  72. \setboolean{enablecjk}{true}
  73. }{}
  74. }{}
  75. \ifthenelse{\boolean{enablecjk}}{
  76. % Load xeCJK when both the Noto Sans CJK font and xeCJK.sty are available.
  77. \usepackage{xeCJK}
  78. % Noto CJK fonts don't provide slant shape. [AutoFakeSlant] permits
  79. % its emulation.
  80. % Select KR variant at the beginning of each document so that quotation
  81. % and apostorph symbols of half-width is used in TOC of Latin documents.
  82. \IfFontExistsTF{Noto Serif CJK KR}{
  83. \setCJKmainfont{Noto Serif CJK KR}[AutoFakeSlant]
  84. }{
  85. \setCJKmainfont{Noto Sans CJK KR}[AutoFakeSlant]
  86. }
  87. \setCJKsansfont{Noto Sans CJK KR}[AutoFakeSlant]
  88. \setCJKmonofont{Noto Sans Mono CJK KR}[AutoFakeSlant]
  89. % Teach xeCJK of half-width symbols
  90. \xeCJKDeclareCharClass{HalfLeft}{`“,`‘}
  91. \xeCJKDeclareCharClass{HalfRight}{`”,`’}
  92. % CJK Language-specific font choices
  93. %% for Simplified Chinese
  94. \IfFontExistsTF{Noto Serif CJK SC}{
  95. \newCJKfontfamily[SCmain]\scmain{Noto Serif CJK SC}[AutoFakeSlant]
  96. \newCJKfontfamily[SCserif]\scserif{Noto Serif CJK SC}[AutoFakeSlant]
  97. }{
  98. \newCJKfontfamily[SCmain]\scmain{Noto Sans CJK SC}[AutoFakeSlant]
  99. \newCJKfontfamily[SCserif]\scserif{Noto Sans CJK SC}[AutoFakeSlant]
  100. }
  101. \newCJKfontfamily[SCsans]\scsans{Noto Sans CJK SC}[AutoFakeSlant]
  102. \newCJKfontfamily[SCmono]\scmono{Noto Sans Mono CJK SC}[AutoFakeSlant]
  103. %% for Traditional Chinese
  104. \IfFontExistsTF{Noto Serif CJK TC}{
  105. \newCJKfontfamily[TCmain]\tcmain{Noto Serif CJK TC}[AutoFakeSlant]
  106. \newCJKfontfamily[TCserif]\tcserif{Noto Serif CJK TC}[AutoFakeSlant]
  107. }{
  108. \newCJKfontfamily[TCmain]\tcmain{Noto Sans CJK TC}[AutoFakeSlant]
  109. \newCJKfontfamily[TCserif]\tcserif{Noto Sans CJK TC}[AutoFakeSlant]
  110. }
  111. \newCJKfontfamily[TCsans]\tcsans{Noto Sans CJK TC}[AutoFakeSlant]
  112. \newCJKfontfamily[TCmono]\tcmono{Noto Sans Mono CJK TC}[AutoFakeSlant]
  113. %% for Korean
  114. \IfFontExistsTF{Noto Serif CJK KR}{
  115. \newCJKfontfamily[KRmain]\krmain{Noto Serif CJK KR}[AutoFakeSlant]
  116. \newCJKfontfamily[KRserif]\krserif{Noto Serif CJK KR}[AutoFakeSlant]
  117. }{
  118. \newCJKfontfamily[KRmain]\krmain{Noto Sans CJK KR}[AutoFakeSlant]
  119. \newCJKfontfamily[KRserif]\krserif{Noto Sans CJK KR}[AutoFakeSlant]
  120. }
  121. \newCJKfontfamily[KRsans]\krsans{Noto Sans CJK KR}[AutoFakeSlant]
  122. \newCJKfontfamily[KRmono]\krmono{Noto Sans Mono CJK KR}[AutoFakeSlant]
  123. %% for Japanese
  124. \IfFontExistsTF{Noto Serif CJK JP}{
  125. \newCJKfontfamily[JPmain]\jpmain{Noto Serif CJK JP}[AutoFakeSlant]
  126. \newCJKfontfamily[JPserif]\jpserif{Noto Serif CJK JP}[AutoFakeSlant]
  127. }{
  128. \newCJKfontfamily[JPmain]\jpmain{Noto Sans CJK JP}[AutoFakeSlant]
  129. \newCJKfontfamily[JPserif]\jpserif{Noto Sans CJK JP}[AutoFakeSlant]
  130. }
  131. \newCJKfontfamily[JPsans]\jpsans{Noto Sans CJK JP}[AutoFakeSlant]
  132. \newCJKfontfamily[JPmono]\jpmono{Noto Sans Mono CJK JP}[AutoFakeSlant]
  133. % Define custom macros to on/off CJK
  134. %% One and half spacing for CJK contents
  135. \newcommand{\kerneldocCJKon}{\makexeCJKactive\onehalfspacing}
  136. \newcommand{\kerneldocCJKoff}{\makexeCJKinactive\singlespacing}
  137. % Define custom macros for switching CJK font setting
  138. %% for Simplified Chinese
  139. \newcommand{\kerneldocBeginSC}{%
  140. \begingroup%
  141. \scmain%
  142. \xeCJKDeclareCharClass{FullLeft}{`“,`‘}% Full-width in SC
  143. \xeCJKDeclareCharClass{FullRight}{`”,`’}% Full-width in SC
  144. \renewcommand{\CJKrmdefault}{SCserif}%
  145. \renewcommand{\CJKsfdefault}{SCsans}%
  146. \renewcommand{\CJKttdefault}{SCmono}%
  147. \xeCJKsetup{CJKspace = false}% gobble white spaces by ' '
  148. % For CJK ascii-art alignment
  149. \setmonofont{Noto Sans Mono CJK SC}[AutoFakeSlant]%
  150. }
  151. \newcommand{\kerneldocEndSC}{\endgroup}
  152. %% for Traditional Chinese
  153. \newcommand{\kerneldocBeginTC}{%
  154. \begingroup%
  155. \tcmain%
  156. \xeCJKDeclareCharClass{FullLeft}{`“,`‘}% Full-width in TC
  157. \xeCJKDeclareCharClass{FullRight}{`”,`’}% Full-width in TC
  158. \renewcommand{\CJKrmdefault}{TCserif}%
  159. \renewcommand{\CJKsfdefault}{TCsans}%
  160. \renewcommand{\CJKttdefault}{TCmono}%
  161. \xeCJKsetup{CJKspace = false}% gobble white spaces by ' '
  162. % For CJK ascii-art alignment
  163. \setmonofont{Noto Sans Mono CJK TC}[AutoFakeSlant]%
  164. }
  165. \newcommand{\kerneldocEndTC}{\endgroup}
  166. %% for Korean
  167. \newcommand{\kerneldocBeginKR}{%
  168. \begingroup%
  169. \krmain%
  170. \renewcommand{\CJKrmdefault}{KRserif}%
  171. \renewcommand{\CJKsfdefault}{KRsans}%
  172. \renewcommand{\CJKttdefault}{KRmono}%
  173. % \xeCJKsetup{CJKspace = true} % true by default
  174. % For CJK ascii-art alignment (still misaligned for Hangul)
  175. \setmonofont{Noto Sans Mono CJK KR}[AutoFakeSlant]%
  176. }
  177. \newcommand{\kerneldocEndKR}{\endgroup}
  178. %% for Japanese
  179. \newcommand{\kerneldocBeginJP}{%
  180. \begingroup%
  181. \jpmain%
  182. \renewcommand{\CJKrmdefault}{JPserif}%
  183. \renewcommand{\CJKsfdefault}{JPsans}%
  184. \renewcommand{\CJKttdefault}{JPmono}%
  185. \xeCJKsetup{CJKspace = false}% gobble white space by ' '
  186. % For CJK ascii-art alignment
  187. \setmonofont{Noto Sans Mono CJK JP}[AutoFakeSlant]%
  188. }
  189. \newcommand{\kerneldocEndJP}{\endgroup}
  190. % Single spacing in literal blocks
  191. \fvset{baselinestretch=1}
  192. % To customize \sphinxtableofcontents
  193. \usepackage{etoolbox}
  194. % Inactivate CJK after tableofcontents
  195. \apptocmd{\sphinxtableofcontents}{\kerneldocCJKoff}{}{}
  196. \xeCJKsetup{CJKspace = true}% For inter-phrase space of Korean TOC
  197. % Suppress extra white space at latin .. non-latin in literal blocks
  198. \AtBeginEnvironment{sphinxVerbatim}{\CJKsetecglue{}}
  199. }{ % Don't enable CJK
  200. % Custom macros to on/off CJK and switch CJK fonts (Dummy)
  201. \newcommand{\kerneldocCJKon}{}
  202. \newcommand{\kerneldocCJKoff}{}
  203. %% By defining \kerneldocBegin(SC|TC|KR|JP) as commands with an argument
  204. %% and ignore the argument (#1) in their definitions, whole contents of
  205. %% CJK chapters can be ignored.
  206. \newcommand{\kerneldocBeginSC}[1]{%
  207. %% Put a note on missing CJK fonts or the xecjk package in place of
  208. %% zh_CN translation.
  209. \begin{sphinxadmonition}{note}{Note on missing fonts and a package:}
  210. Translations of Simplified Chinese (zh\_CN), Traditional Chinese
  211. (zh\_TW), Korean (ko\_KR), and Japanese (ja\_JP) were skipped
  212. due to the lack of suitable font families and/or the texlive-xecjk
  213. package.
  214. If you want them, please install non-variable ``Noto Sans CJK''
  215. font families along with the texlive-xecjk package by following
  216. instructions from
  217. \sphinxcode{./scripts/sphinx-pre-install}.
  218. Having optional non-variable ``Noto Serif CJK'' font families will
  219. improve the looks of those translations.
  220. \end{sphinxadmonition}}
  221. \newcommand{\kerneldocEndSC}{}
  222. \newcommand{\kerneldocBeginTC}[1]{}
  223. \newcommand{\kerneldocEndTC}{}
  224. \newcommand{\kerneldocBeginKR}[1]{}
  225. \newcommand{\kerneldocEndKR}{}
  226. \newcommand{\kerneldocBeginJP}[1]{}
  227. \newcommand{\kerneldocEndJP}{}
  228. }