kerneldoc-preamble.sty 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. %% Sphinx < 1.8 doesn't have \sphinxtableofcontentshook
  57. \providecommand{\sphinxtableofcontentshook}{}
  58. %% Undefine it for compatibility with Sphinx 1.7.9
  59. \renewcommand{\sphinxtableofcontentshook}{} % Empty the hook
  60. % Prevent column squeezing of tabulary. \tymin is set by Sphinx as:
  61. % \setlength{\tymin}{3\fontcharwd\font`0 }
  62. % , which is too short.
  63. \setlength{\tymin}{20em}
  64. % Adjust \headheight for fancyhdr
  65. \addtolength{\headheight}{1.6pt}
  66. \addtolength{\topmargin}{-1.6pt}
  67. % Translations have Asian (CJK) characters which are only displayed if
  68. % xeCJK is used
  69. \usepackage{ifthen}
  70. \newboolean{enablecjk}
  71. \setboolean{enablecjk}{false}
  72. \IfFontExistsTF{Noto Sans CJK SC}{
  73. \IfFileExists{xeCJK.sty}{
  74. \setboolean{enablecjk}{true}
  75. }{}
  76. }{}
  77. \ifthenelse{\boolean{enablecjk}}{
  78. % Load xeCJK when both the Noto Sans CJK font and xeCJK.sty are available.
  79. \usepackage{xeCJK}
  80. % Noto CJK fonts don't provide slant shape. [AutoFakeSlant] permits
  81. % its emulation.
  82. % Select KR variant at the beginning of each document so that quotation
  83. % and apostorph symbols of half-width is used in TOC of Latin documents.
  84. \IfFontExistsTF{Noto Serif CJK KR}{
  85. \setCJKmainfont{Noto Serif CJK KR}[AutoFakeSlant]
  86. }{
  87. \setCJKmainfont{Noto Sans CJK KR}[AutoFakeSlant]
  88. }
  89. \setCJKsansfont{Noto Sans CJK KR}[AutoFakeSlant]
  90. \setCJKmonofont{Noto Sans Mono CJK KR}[AutoFakeSlant]
  91. % Teach xeCJK of half-width symbols
  92. \xeCJKDeclareCharClass{HalfLeft}{`“,`‘}
  93. \xeCJKDeclareCharClass{HalfRight}{`”,`’}
  94. % CJK Language-specific font choices
  95. %% for Simplified Chinese
  96. \IfFontExistsTF{Noto Serif CJK SC}{
  97. \newCJKfontfamily[SCmain]\scmain{Noto Serif CJK SC}[AutoFakeSlant]
  98. \newCJKfontfamily[SCserif]\scserif{Noto Serif CJK SC}[AutoFakeSlant]
  99. }{
  100. \newCJKfontfamily[SCmain]\scmain{Noto Sans CJK SC}[AutoFakeSlant]
  101. \newCJKfontfamily[SCserif]\scserif{Noto Sans CJK SC}[AutoFakeSlant]
  102. }
  103. \newCJKfontfamily[SCsans]\scsans{Noto Sans CJK SC}[AutoFakeSlant]
  104. \newCJKfontfamily[SCmono]\scmono{Noto Sans Mono CJK SC}[AutoFakeSlant]
  105. %% for Traditional Chinese
  106. \IfFontExistsTF{Noto Serif CJK TC}{
  107. \newCJKfontfamily[TCmain]\tcmain{Noto Serif CJK TC}[AutoFakeSlant]
  108. \newCJKfontfamily[TCserif]\tcserif{Noto Serif CJK TC}[AutoFakeSlant]
  109. }{
  110. \newCJKfontfamily[TCmain]\tcmain{Noto Sans CJK TC}[AutoFakeSlant]
  111. \newCJKfontfamily[TCserif]\tcserif{Noto Sans CJK TC}[AutoFakeSlant]
  112. }
  113. \newCJKfontfamily[TCsans]\tcsans{Noto Sans CJK TC}[AutoFakeSlant]
  114. \newCJKfontfamily[TCmono]\tcmono{Noto Sans Mono CJK TC}[AutoFakeSlant]
  115. %% for Korean
  116. \IfFontExistsTF{Noto Serif CJK KR}{
  117. \newCJKfontfamily[KRmain]\krmain{Noto Serif CJK KR}[AutoFakeSlant]
  118. \newCJKfontfamily[KRserif]\krserif{Noto Serif CJK KR}[AutoFakeSlant]
  119. }{
  120. \newCJKfontfamily[KRmain]\krmain{Noto Sans CJK KR}[AutoFakeSlant]
  121. \newCJKfontfamily[KRserif]\krserif{Noto Sans CJK KR}[AutoFakeSlant]
  122. }
  123. \newCJKfontfamily[KRsans]\krsans{Noto Sans CJK KR}[AutoFakeSlant]
  124. \newCJKfontfamily[KRmono]\krmono{Noto Sans Mono CJK KR}[AutoFakeSlant]
  125. %% for Japanese
  126. \IfFontExistsTF{Noto Serif CJK JP}{
  127. \newCJKfontfamily[JPmain]\jpmain{Noto Serif CJK JP}[AutoFakeSlant]
  128. \newCJKfontfamily[JPserif]\jpserif{Noto Serif CJK JP}[AutoFakeSlant]
  129. }{
  130. \newCJKfontfamily[JPmain]\jpmain{Noto Sans CJK JP}[AutoFakeSlant]
  131. \newCJKfontfamily[JPserif]\jpserif{Noto Sans CJK JP}[AutoFakeSlant]
  132. }
  133. \newCJKfontfamily[JPsans]\jpsans{Noto Sans CJK JP}[AutoFakeSlant]
  134. \newCJKfontfamily[JPmono]\jpmono{Noto Sans Mono CJK JP}[AutoFakeSlant]
  135. % Dummy commands for Sphinx < 2.3 (no 'extrapackages' support)
  136. \providecommand{\onehalfspacing}{}
  137. \providecommand{\singlespacing}{}
  138. % Define custom macros to on/off CJK
  139. %% One and half spacing for CJK contents
  140. \newcommand{\kerneldocCJKon}{\makexeCJKactive\onehalfspacing}
  141. \newcommand{\kerneldocCJKoff}{\makexeCJKinactive\singlespacing}
  142. % Define custom macros for switching CJK font setting
  143. %% for Simplified Chinese
  144. \newcommand{\kerneldocBeginSC}{%
  145. \begingroup%
  146. \scmain%
  147. \xeCJKDeclareCharClass{FullLeft}{`“,`‘}% Full-width in SC
  148. \xeCJKDeclareCharClass{FullRight}{`”,`’}% Full-width in SC
  149. \renewcommand{\CJKrmdefault}{SCserif}%
  150. \renewcommand{\CJKsfdefault}{SCsans}%
  151. \renewcommand{\CJKttdefault}{SCmono}%
  152. \xeCJKsetup{CJKspace = false}% gobble white spaces by ' '
  153. % For CJK ascii-art alignment
  154. \setmonofont{Noto Sans Mono CJK SC}[AutoFakeSlant]%
  155. }
  156. \newcommand{\kerneldocEndSC}{\endgroup}
  157. %% for Traditional Chinese
  158. \newcommand{\kerneldocBeginTC}{%
  159. \begingroup%
  160. \tcmain%
  161. \xeCJKDeclareCharClass{FullLeft}{`“,`‘}% Full-width in TC
  162. \xeCJKDeclareCharClass{FullRight}{`”,`’}% Full-width in TC
  163. \renewcommand{\CJKrmdefault}{TCserif}%
  164. \renewcommand{\CJKsfdefault}{TCsans}%
  165. \renewcommand{\CJKttdefault}{TCmono}%
  166. \xeCJKsetup{CJKspace = false}% gobble white spaces by ' '
  167. % For CJK ascii-art alignment
  168. \setmonofont{Noto Sans Mono CJK TC}[AutoFakeSlant]%
  169. }
  170. \newcommand{\kerneldocEndTC}{\endgroup}
  171. %% for Korean
  172. \newcommand{\kerneldocBeginKR}{%
  173. \begingroup%
  174. \krmain%
  175. \renewcommand{\CJKrmdefault}{KRserif}%
  176. \renewcommand{\CJKsfdefault}{KRsans}%
  177. \renewcommand{\CJKttdefault}{KRmono}%
  178. % \xeCJKsetup{CJKspace = true} % true by default
  179. % For CJK ascii-art alignment (still misaligned for Hangul)
  180. \setmonofont{Noto Sans Mono CJK KR}[AutoFakeSlant]%
  181. }
  182. \newcommand{\kerneldocEndKR}{\endgroup}
  183. %% for Japanese
  184. \newcommand{\kerneldocBeginJP}{%
  185. \begingroup%
  186. \jpmain%
  187. \renewcommand{\CJKrmdefault}{JPserif}%
  188. \renewcommand{\CJKsfdefault}{JPsans}%
  189. \renewcommand{\CJKttdefault}{JPmono}%
  190. \xeCJKsetup{CJKspace = false}% gobble white space by ' '
  191. % For CJK ascii-art alignment
  192. \setmonofont{Noto Sans Mono CJK JP}[AutoFakeSlant]%
  193. }
  194. \newcommand{\kerneldocEndJP}{\endgroup}
  195. % Single spacing in literal blocks
  196. \fvset{baselinestretch=1}
  197. % To customize \sphinxtableofcontents
  198. \usepackage{etoolbox}
  199. % Inactivate CJK after tableofcontents
  200. \apptocmd{\sphinxtableofcontents}{\kerneldocCJKoff}{}{}
  201. \xeCJKsetup{CJKspace = true}% For inter-phrase space of Korean TOC
  202. }{ % Don't enable CJK
  203. % Custom macros to on/off CJK and switch CJK fonts (Dummy)
  204. \newcommand{\kerneldocCJKon}{}
  205. \newcommand{\kerneldocCJKoff}{}
  206. %% By defining \kerneldocBegin(SC|TC|KR|JP) as commands with an argument
  207. %% and ignore the argument (#1) in their definitions, whole contents of
  208. %% CJK chapters can be ignored.
  209. \newcommand{\kerneldocBeginSC}[1]{%
  210. %% Put a note on missing CJK fonts or the xecjk package in place of
  211. %% zh_CN translation.
  212. \begin{sphinxadmonition}{note}{Note on missing fonts and a package:}
  213. Translations of Simplified Chinese (zh\_CN), Traditional Chinese
  214. (zh\_TW), Korean (ko\_KR), and Japanese (ja\_JP) were skipped
  215. due to the lack of suitable font families and/or the texlive-xecjk
  216. package.
  217. If you want them, please install ``Noto Sans CJK'' font families
  218. along with the texlive-xecjk package by following instructions from
  219. \sphinxcode{./scripts/sphinx-pre-install}.
  220. Having optional ``Noto Serif CJK'' font families will improve
  221. the looks of those translations.
  222. \end{sphinxadmonition}}
  223. \newcommand{\kerneldocEndSC}{}
  224. \newcommand{\kerneldocBeginTC}[1]{}
  225. \newcommand{\kerneldocEndTC}{}
  226. \newcommand{\kerneldocBeginKR}[1]{}
  227. \newcommand{\kerneldocEndKR}{}
  228. \newcommand{\kerneldocBeginJP}[1]{}
  229. \newcommand{\kerneldocEndJP}{}
  230. }