diff options
Diffstat (limited to 'macros/latex/contrib/nostarch/nostarch.dtx')
-rw-r--r-- | macros/latex/contrib/nostarch/nostarch.dtx | 308 |
1 files changed, 290 insertions, 18 deletions
diff --git a/macros/latex/contrib/nostarch/nostarch.dtx b/macros/latex/contrib/nostarch/nostarch.dtx index d39fb87722..56969391d5 100644 --- a/macros/latex/contrib/nostarch/nostarch.dtx +++ b/macros/latex/contrib/nostarch/nostarch.dtx @@ -474,16 +474,21 @@ % captions. The captions for figures go \emph{below} the figures, the % captions for tables go \emph{above} the tabular data. % -% If a caption for a table or figure turns out to be wider than the -% body, you might want to split the caption into lines. Remember, -% however, that the mandatory argument to |\caption| becomes -% ``robust'' in the \LaTeX{} parlance~\cite{Lamport94} only when there -% is the optional one. Therefore the correct way to split a caption -% is the following: +% \DescribeMacro{\NextCaptionWidth}% +% The captions of tables and figures are automatically typeset with +% the same width as the table or figure. Sometimes, however, \LaTeX\ +% makes a mistake in the calculation. In this case the macro +% \cs{NextCaptionWidth}\marg{length} overrides this calculation and +% sets the width of the caption for the next figure or table to +% \marg{length}, for example % \begin{verbatim} -% \caption[A Long Caption With Lines To Split]{% -% A Long Caption\\ With Lines To Split} +% \NextCaptionWidth{2in} +% \begin{figure} +% ... +% \caption{...} +% \end{figure} % \end{verbatim} +% % % Typesetting tables, unfortunately, is rarely done right, and % standard \LaTeX{} is not an exception. The authors are urged to @@ -583,6 +588,18 @@ % example, in quote characters. % % +%\subsection{Boxes} +%\label{sec:ug_boxes} +% +% \DescribeEnv{nspbox}% +% You may put some information into \emph{boxes}. Boxes have a \emph{heading} +% and a \emph{text}. The environment |nspbox| can be used to typeset +% them, for example: +% \begin{verbatim} +% \begin{nspbox}{This is a heading} +% This is a text for the box. +% \end{nspbox} +% \end{verbatim} % %\subsection{Back Matter} %\label{sec:guide:backmatter} @@ -686,7 +703,7 @@ %<class>\ProvidesClass{nostarch} %<nshyper>\ProvidesPackage{nshyper} %<*!ist> -[2023/03/30 v2.0 Typesetting books for No Starch Press] +[2023/04/08 v2.1 Typesetting books for No Starch Press] %</!ist> % \end{macrocode} % @@ -783,10 +800,11 @@ pagecolor=black,urlcolor=black,hyperindex=false]{hyperref} % A bunch of packages: % \begin{macrocode} \RequirePackage[table]{xcolor} -\RequirePackage{iftex, ifpdf, fancyhdr, fancyvrb, booktabs, graphicx, +\RequirePackage{iftex, ifpdf, longtable, + fancyhdr, fancyvrb, booktabs, graphicx, listings, caption, makeidx, upquote, pifont, textcomp, xcolor, lmodern, nameref, float, amssymb, etoolbox, - setspace, marginnote} + setspace, marginnote, url} \RequirePackage[framemethod=TikZ]{mdframed} \RequirePackage{soul} \RequirePackage[T1]{fontenc} @@ -2377,17 +2395,189 @@ pagecolor=black,urlcolor=black,hyperindex=false]{hyperref} % \end{macrocode} % \end{macro} % +% Setting up longtables +% \begin{macrocode} +\def\LTleft{\z@} +\def\LTright{\fill} +% \end{macrocode} +% +% \begin{macro}{\ifnostarch@overridecaptionwidth} +% \changes{v2.1}{2023/04/09}{Introduced macro} +% Whether the user wants to override automatically selected caption +% width +% \begin{macrocode} +\newif\ifnostarch@overridecaptionwidth +\nostarch@overridecaptionwidthfalse +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\nostarch@captionwidth} +% \changes{v2.1}{2023/04/09}{Introduced macro} +% The width of the caption. Either set by the user or measured +% \begin{macrocode} +\newlength\nostarch@captionwidth +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\NextCaptionWidth} +% \changes{v2.1}{2023/04/09}{Introduced macro} +% A user-level command to set the caption width +% \begin{macrocode} +\newcommand\NextCaptionWidth[1]{% + \global\nostarch@overridecaptionwidthtrue + \global\setlength{\nostarch@captionwidth}{#1}% +} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\nostarch@measurecaptionwidth} +% \changes{v2.1}{2023/04/09}{Introduced macro} +% We set \cs{nostarch@captionwidth} to the width of the last box +% unless the flag \cs{ifnostarch@overridecaptionwidth} is set. At the +% end we set the flag to false. +% \begin{macrocode} +\newcommand\nostarch@measurecaptionwidth{% + \ifnostarch@overridecaptionwidth\else + \par + \setbox\@tempboxa\lastbox + \setbox\@tempboxa=\hbox{\unhbox\@tempboxa}% + \global\setlength{\nostarch@captionwidth}{\wd\@tempboxa}% + \box\@tempboxa\par + \fi + \global\nostarch@overridecaptionwidthfalse +} +% \end{macrocode} +% \end{macro} +% +% We measure the width at the begin of the figure caption. Note that +% the cs{AtBeginCaption} hook is too late for us. +% \begin{macrocode} +\AtBeginDocument{% +\pretocmd{\caption}{\def\@tempa{figure}% + \ifx\@tempa\@captype\relax + \nostarch@measurecaptionwidth + \fi}{}{}} +% \end{macrocode} +% +% +% We declare the caption format for figures and tables +% \begin{macrocode} +\DeclareCaptionFormat*{nostarchfigtblformat}{% + \parbox{\nostarch@captionwidth}{#1#2#3}} +% \end{macrocode} +% +% Tables are more complex, because captions are \emph{before} the +% tables. Therefore we need to write the width into aux file and read +% it from there. Most of the ideas here are stolen from +% \textsl{longtable} code. +% +% \begin{macro}{\nostarch@tbl@warn} +% \changes{v2.1}{2023/04/10}{Introduced macro} +% Whether to ask the user for rerun due to changed widths +% \begin{macrocode} +\def\nostarch@tbl@warn{} +\AtEndDocument{\nostarch@tbl@warn} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\c@nostarch@tbl} +% \changes{v2.1}{2023/04/10}{Introduced macro} +% We write the table width into the macro \cs{nostarch@tbl@N}, where +% $N$ is the roman representation of the table number +% \begin{macrocode} +\newcounter{nostarch@tbl} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\nostarch@read@tbl@width} +% \changes{v2.1}{2023/04/10}{Introduced macro} +% At the beginning of table environment we read the latest value of the +% width for the given table and set the caption size +% \begin{macrocode} +\newcommand\nostarch@read@tbl@width{% + \stepcounter{nostarch@tbl}% + \expandafter\ifx\csname nostarch@tbl@\romannumeral\c@nostarch@tbl\endcsname\relax + \edef\@tempa{\the\textwidth}% + \else + \edef\@tempa{\csname nostarch@tbl@\romannumeral\c@nostarch@tbl\endcsname}% + \fi + \expandafter\setlength\expandafter\nostarch@captionwidth\expandafter{\@tempa}% +} +\AtBeginEnvironment{table}{\nostarch@read@tbl@width} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\nostarch@save@tbl@width} +% \changes{v2.1}{2023/04/10}{Introduced macro} +% At the end of the table environment we measure the width of the last +% box and write it in the aux file. +% \begin{macrocode} +\newcommand\nostarch@save@tbl@width{% + \nostarch@measurecaptionwidth + \if@filesw + \immediate\write\@auxout{% + \gdef\expandafter\noexpand\csname nostarch@tbl@\romannumeral\c@nostarch@tbl\endcsname + {\the\nostarch@captionwidth}}% + \fi + \expandafter\ifx\csname nostarch@tbl@\romannumeral\c@nostarch@tbl\endcsname\relax + \edef\@tempa{\the\textwidth}% + \else + \edef\@tempa{\csname nostarch@tbl@\romannumeral\c@nostarch@tbl\endcsname}% + \fi + \expandafter\ifdim\@tempa=\nostarch@captionwidth\else + \gdef\nostarch@tbl@warn{\ClassWarningNoLine{nostarch} + {Table widths have changed. Rerun LaTeX.}} + \fi +} +\pretocmd{\endtable}{\nostarch@save@tbl@width}{}{} +% \end{macrocode} +% \end{macro} +% +% The |H| specifier from the |float| package redefines the end of the +% float, so we need to patch it too +% \begin{macrocode} +\pretocmd{\float@endH}{\def\@tempa{table}% + \ifx\@tempa\@captype\nostarch@save@tbl@width\fi}{}{} +% \end{macrocode} +% +% \begin{macro}{\nostarch@read@LT@width} +% \changes{v2.1}{2023/04/10}{Introduced macro} +% The long table version of setting caption width is different, +% because a longtable knows its width. +% \begin{macrocode} +\def\nostarch@read@LT@width{% + \ifnostarch@overridecaptionwidth\else + \global\setlength{\nostarch@captionwidth}{\z@}% + \bgroup + \def\LT@entry##1##2{% + \global\addtolength{\nostarch@captionwidth}{##2}}% + \csname LT@\romannumeral\c@LT@tables\endcsname + \egroup + \fi + \global\nostarch@overridecaptionwidthfalse +} +\AtBeginDocument{\pretocmd{\caption@LT@config}{\global\rownum=1\relax + \nostarch@read@LT@width}{}{}} +\AtBeginEnvironment{longtable}{\global\rownum=0\relax} +% \end{macrocode} +% \end{macro} +% % % Fonts for captions % \begin{macrocode} -\DeclareCaptionFont{nostarchfigfont}{\sffamily \itshape \fontsize{8.5pt}{9pt}\selectfont} -\DeclareCaptionFont{nostarchtabfont}{\futurabook \fontsize{9pt}{9pt}\selectfont} +\DeclareCaptionFont{nostarchfigfont}{\sffamily + \itshape \fontsize{8.5pt}{9pt}\selectfont} +\DeclareCaptionFont{nostarchtabfont}{\futurabook + \fontsize{9pt}{9pt}\selectfont} +% \end{macrocode} +% And the setup +% \begin{macrocode} \captionsetup[figure]{font=nostarchfigfont,singlelinecheck=off, - aboveskip=8pt,belowskip=-8pt} + aboveskip=8pt,belowskip=-8pt, format=nostarchfigtblformat} \captionsetup[lstlisting]{font=nostarchfigfont,singlelinecheck=off, aboveskip=4pt, belowskip=0pt} \captionsetup[table]{font=nostarchtabfont,singlelinecheck=off, - aboveskip=0pt,belowskip=4pt, labelfont=bf} + aboveskip=0pt,belowskip=4pt, labelfont=bf, format=nostarchfigtblformat} % \end{macrocode} % % \begin{macro}{\captionlst} @@ -2417,6 +2607,7 @@ pagecolor=black,urlcolor=black,hyperindex=false]{hyperref} % \end{macro} % \begin{macrocode} \AtBeginEnvironment{tabular}{\tbfont} +\AtBeginEnvironment{longtable}{\tbfont} % \end{macrocode} % % \begin{macro}{\heawyrulewidth} @@ -2509,7 +2700,19 @@ pagecolor=black,urlcolor=black,hyperindex=false]{hyperref} % \end{macrocode} % \end{macro} % -%\subsection{Verbatim and Listings Customization} +% Setting up colors in tables +% \begin{macrocode} +\rowcolors[]{1}{}{nspgray} +% \end{macrocode} +% +% We extend the height of the rows and delete the space arounr rules +% \begin{macrocode} +\setlength{\aboverulesep}{0pt} +\setlength{\belowrulesep}{0pt} +\setlength{\extrarowheight}{.8ex} +% \end{macrocode} +% +%\subsection{Verbatim, url and Listings Customization} %\label{sec:verbatim} % % \begin{macro}{Code} @@ -2556,6 +2759,7 @@ pagecolor=black,urlcolor=black,hyperindex=false]{hyperref} innerrightmargin=18pt, innerleftmargin=18pt, backgroundcolor=nspgray, + everyline=true } \mdfdefinestyle{custombox}{% @@ -2566,6 +2770,7 @@ pagecolor=black,urlcolor=black,hyperindex=false]{hyperref} innerbottommargin=\baselineskip, innerrightmargin=18pt, innerleftmargin=18pt, + everyline=true } \newenvironment{nspbox}[1] {\begin{mdframed}[style=nspbox] @@ -2640,6 +2845,74 @@ pagecolor=black,urlcolor=black,hyperindex=false]{hyperref} } % \end{macrocode} % +% Urls are typeset in italics, with upper tilde, and according to the +% following rules: URLs should break \emph{before} periods, +% underscores, equal signs, ampersands, and hyphens but \emph{after} +% colons, slashes and hash marks. Also, we do not allow splitting +% before slashes. +% \begin{macro}{\UrlFont} +% \changes{v2.1}{2023/04/08}{Redefined macro} +% Urls are in italic +% \begin{macrocode} +\def\UrlFont{\itshape} +% \end{macrocode} +% \end{macro} +% \begin{macro}{\UrlTildeSpecial} +% \changes{v2.1}{2023/04/08}{Redefined macro} +% \begin{macrocode} +\def\UrlTildeSpecial{\do\~{\raise.2ex\hbox{\m@th$\scriptstyle\sim$}}} +\let\Url@force@Tilde\UrlTildeSpecial +% \end{macrocode} +% \end{macro} +% \begin{macro}{\UrlBreaks} +% \changes{v2.1}{2023/04/08}{Redefined macro} +% \changes{v2.1}{2023/04/10}{Moved slashes to specials} +% Breaks after colons, slashes, hash marks. Colons are defined in +% \cs{UrlBigBreaks}, so we need the other signs. We also add some +% other signs here +% \begin{macrocode} +\def\UrlBreaks{\do\#\do\\\do\>\do\]\do\)\do\}} +% \end{macrocode} +% \end{macro} +% \begin{macro}{\UrlSpecials} +% \changes{v2.1}{2023/04/08}{Redefined macro} +% \changes{v2.1}{2023/04/11}{Added slashes and double quote} +% We add to macro special treatment of the chars with breaking URLs. +% Slashes are interesting: we allow breaking after them, but not +% before, even after other slashes or colons +% \emph{before} them +% \begin{macrocode} +\g@addto@macro{\UrlSpecials}{% + \do\.{\penalty\UrlBreakPenalty\mathchar46\relax}% + \do\_{\penalty\UrlBreakPenalty\mathchar95\relax}% + \do\={\penalty\UrlBreakPenalty\mathchar61\relax}% + \do\&{\penalty\UrlBreakPenalty\mathchar38\relax}% + \do\-{\penalty\UrlBreakPenalty\mathchar45\relax}% + \do\!{\penalty\UrlBreakPenalty\mathchar33\relax}% + \do\?{\penalty\UrlBreakPenalty\mathchar63\relax}% + \do\|{\penalty\UrlBreakPenalty\mathchar124\relax}% + \do\,{\penalty\UrlBreakPenalty\mathchar44\relax}% + \do\;{\penalty\UrlBreakPenalty\mathchar59\relax}% + \do\@{\penalty\UrlBreakPenalty\mathchar64\relax}% + \do\'{\penalty\UrlBreakPenalty\mathchar39\relax}% + \do\"{\penalty\UrlBreakPenalty\mathchar34\relax}% + \do\+{\penalty\UrlBreakPenalty\mathchar43\relax}% + \do\<{\penalty\UrlBreakPenalty\mathchar60\relax}% + \do\[{\penalty\UrlBreakPenalty\mathchar91\relax}% + \do\({\penalty\UrlBreakPenalty\mathchar40\relax}% + \do\{{\penalty\UrlBreakPenalty\mathchar123\relax}% + \do\/{\unpenalty\penalty\@M\mathchar47\penalty\UrlBreakPenalty}% +} +% \end{macrocode} +% \end{macro} +% +% \begin{macro}{\UrlNoBreaks} +% \changes{v2.1}{2023/04/08}{Redefined macro} +% We allow breaks in urls after brackets +% \begin{macrocode} +\def\UrlNoBreaks{} +% \end{macrocode} +% \end{macro} % %\subsection{Margin notes} %\label{sec:margins} @@ -2960,8 +3233,7 @@ pagecolor=black,urlcolor=black,hyperindex=false]{hyperref} % \begin{macrocode} %<*nshyper> \RequirePackage[breaklinks,colorlinks,linkcolor=black, - citecolor=black,pagecolor=black,urlcolor=black,hyperindex, - bookmarks=false]{hyperref} + citecolor=black,pagecolor=black,urlcolor=black,hyperindex]{hyperref} % \end{macrocode} % % \progname{hyperref} expects the starred parts and chapters to have |