From 73a1d8782de0159a4443f4b5627df0122ec9d9f1 Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Mon, 15 Nov 2021 21:10:04 +0000 Subject: formal-grammar (15nov21) git-svn-id: svn://tug.org/texlive/trunk@61062 c570f23f-e606-0410-a88d-b1316a301751 --- .../texmf-dist/doc/latex/formal-grammar/README.md | 6 ++ .../doc/latex/formal-grammar/formal-grammar.pdf | Bin 187407 -> 191738 bytes .../source/latex/formal-grammar/formal-grammar.dtx | 101 +++++++++++++-------- .../tex/latex/formal-grammar/formal-grammar.sty | 54 +++++------ 4 files changed, 96 insertions(+), 65 deletions(-) (limited to 'Master/texmf-dist') diff --git a/Master/texmf-dist/doc/latex/formal-grammar/README.md b/Master/texmf-dist/doc/latex/formal-grammar/README.md index 9a8e13b0307..4f02b0b895e 100644 --- a/Master/texmf-dist/doc/latex/formal-grammar/README.md +++ b/Master/texmf-dist/doc/latex/formal-grammar/README.md @@ -17,6 +17,12 @@ references), which is the documentation for the package. This package is provided under the terms of the LaTeX Project Public License, version 1.3 or later. +# Author + +The initial author of this package is Martin Vassor. + +Thanks to Enrico Gregorio (egreg) for suggesting improvements. + # See also - [**simplebnf**](https://www.ctan.org/pkg/simplebnf): this package provides an diff --git a/Master/texmf-dist/doc/latex/formal-grammar/formal-grammar.pdf b/Master/texmf-dist/doc/latex/formal-grammar/formal-grammar.pdf index ae8320e09ab..f68c90ae4bb 100644 Binary files a/Master/texmf-dist/doc/latex/formal-grammar/formal-grammar.pdf and b/Master/texmf-dist/doc/latex/formal-grammar/formal-grammar.pdf differ diff --git a/Master/texmf-dist/source/latex/formal-grammar/formal-grammar.dtx b/Master/texmf-dist/source/latex/formal-grammar/formal-grammar.dtx index 92bf02e8710..da5f2bdae1e 100644 --- a/Master/texmf-dist/source/latex/formal-grammar/formal-grammar.dtx +++ b/Master/texmf-dist/source/latex/formal-grammar/formal-grammar.dtx @@ -16,9 +16,9 @@ % \iffalse %\NeedsTeXFormat{LaTeX2e} -%\ProvidesPackage{formal-grammar}[2021/11/10 v1.0 A package to typeset formal grammars] +%\ProvidesPackage{formal-grammar}[2021/11/15 v1.1 A package to typeset formal grammars] %\RequirePackage{xparse} -%\RequirePackage{float} +%\RequirePackage{newfloat} %\RequirePackage{xcolor} %\RequirePackage{colortbl} %\RequirePackage{array} @@ -26,6 +26,7 @@ %<*driver> \documentclass{ltxdoc} \usepackage{formal-grammar} +\usepackage[hidelinks]{hyperref} \EnableCrossrefs \CodelineIndex \RecordChanges @@ -35,7 +36,7 @@ % % \fi % -% \CheckSum{83} +% \CheckSum{97} % % \CharacterTable % {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 @@ -54,7 +55,8 @@ % Right brace \} Tilde \~} % % -% \changes{v1.0}{2021/11/21}{Initial version for publication} +% \changes{v1.1}{2021/11/15}{Use Coloneqq if defined, internal changes, typos} +% \changes{v1.0}{2021/11/10}{Initial version for publication} % % \GetFileInfo{formal-grammar.sty} % @@ -75,7 +77,7 @@ % \section{Introduction} % % This package provides a new environment (|grammar|) and associated commands to -% typeset BNF grammars. It allows to easily write formal grammars, for instance +% typeset BNF grammars. It allows to easily write formal grammars. For instance, % the syntax of the \(\lambda\) calculus is given in Grammar~\ref{gr:lambda}. % %\begin{grammar}[\(\lambda\) calculus syntax][][gr:lambda] @@ -99,7 +101,7 @@ % optional arguments: the first one is a possible caption; the second is a % positionning option; and the third is a label. % -% If none of the optional arguments is provided, the grammar is inlined (i.e. +% If none of the optional arguments are provided, the grammar is inlined (i.e. % not in a float environment. If the first argument is set (the optional % caption), the grammar is typeset in a float, captionned with the provided % caption. @@ -131,7 +133,7 @@ % \otherform{\{\}}{Curly brackets} % \end{grammar} % -% \paragraph{Referencing non-terminals} +% \paragraph{Referencing non-terminals.} % % \DescribeMacro{\nonterm} % This allows you to typeset a symbol as a non-terminal. In the current version, @@ -167,8 +169,8 @@ % \begin{verbatim} % \firstcase{B}{(\nonterm{B})\gralt \{\nonterm{B}\}}{Nested parenthesis or brackets} % \end{verbatim} -% \paragraph{Subtle typesetting of non-terminals} -% Since nonterminal are, by default, typeset using \verb+\mathcal+, it can lead +% \paragraph{Subtle typesetting of non-terminals.} +% Since nonterminals are, by default, typeset using \verb+\mathcal+, it can lead % to the usual issues of \verb+\mathcal+ (typically, for lowercases). Therefore, we provide \emph{subtle} % variants of |\firstcase| and |\nonterm|, in which the non-terminal symbol is % not typeset (i.e. as the user, you have to typeset it manually). @@ -182,7 +184,7 @@ % \verb+\(\nontermsubtil{\nonterm{C}_1}\)+ % % \DescribeMacro{\firstcasesubtil} -% The subtil variant of |\firstcase|. Works similarly, except that the +% The subtle variant of |\firstcase|. It works similarly, except that the % non-terminal (i.e. the first argument) is not embedded in a \verb+\mathcal+ % macro. For instance, the \(\nontermsubtil{\nonterm{C}_1}\) in % Grammar~\ref{gr:advanced_grammar} is typeset with the following command: @@ -190,8 +192,8 @@ % \firstcasesubtil{\(\nonterm{C}_1\)}{\nonterm{B}}{Example of subtil non-terminal} % \end{verbatim} % -% \paragraph{Highlighting and downplaying variants} -% Three commands are provided to highlight or downplay some part of a grammar. +% \paragraph{Highlighting and downplaying variants.} +% Three commands are provided to highlight or downplay some parts of a grammar. % |\highlight| highlights a whole line, |\loghighlight| highlights a part of a % line, while |\downplay| downplays a line. % @@ -219,6 +221,15 @@ % \gralt \nonterm{A}}{Important item} % \end{verbatim} % +% \paragraph{Customizing the \(::=\) symbol.} +% At the end of the preamble (i.e. before the \verb+\begin{document}+), the +% package checks if a command \verb+\Coloneqq+ is defined. If that is the case, +% it is used instead of \(::=\). Typically, packages +% \href{https://www.ctan.org/pkg/mathtools}{mathtools}, +% \href{https://www.ctan.org/pkg/txfonts}{txfonts} and +% \href{https://www.ctan.org/pkg/pxfonts}{pxfonts} define this command, but you +% can also define it manually if you use the symbol elsewhere in the document. +% % \StopEventually{\PrintIndex} % % \section{Implementation} @@ -227,8 +238,11 @@ % This is a new float that contains floating grammars. This is needed so that % they are labeled with 'Grammar'. % \begin{macrocode} -\newfloat{floatgrammar}{t b h p}{.gram} -\floatname{floatgrammar}{Grammar} + \DeclareFloatingEnvironment[ + name=Grammar, + listname={List of Grammars}, + placement=tbhp, + ]{floatgrammar} % \end{macrocode} % \end{environment} % @@ -236,31 +250,34 @@ %% Taken from https://tex.stackexchange.com/a/26364/107341 %\fi % -% \begin{macro}{\@rowstyle} +% \begin{macro}{\formal@rowstyle} % The default \verb+rowstyle+ is empty. % \begin{macrocode} -\newcommand*{\@rowstyle}{} +\newcommand*{\formal@rowstyle}{} % \end{macrocode} % \end{macro} % % \begin{macro}{\rowstyle} -% An internal command used to set the style of a row. In addition, we add column +% An command used to set the style of a row. In addition, we add column % types to reset the style (\verb:=:) and to keep the style from one column to % the other (\verb:+:). +% As of today, it is not advised for the user to use \verb+\rowstyle+ to define +% their own style (i.e.\ I have not tested it), although I hope it will someday +% be possible. % \begin{macrocode} \newcommand*{\rowstyle}[1]{% sets the style of the next row - \gdef\@rowstyle{#1}% - \@rowstyle\ignorespaces% + \gdef\formal@rowstyle{#1}% + \formal@rowstyle\ignorespaces% } % \end{macrocode} % \begin{macrocode} -\newcolumntype{=}{% resets the row style - >{\gdef\@rowstyle{}}% +\newcolumntype{\formal@reset}{% resets the row style + >{\gdef\formal@rowstyle{}}% } -\newcolumntype{+}{% adds the current row style to the next column - >{\@rowstyle}% +\newcolumntype{\formal@add}{% adds the current row style to the next column + >{\formal@rowstyle}% } % \end{macrocode} % \end{macro} @@ -274,33 +291,27 @@ % we embed the grammar into a \verb+floatgrammar+; then if the second argument % is provided, we use it as the position, (otherwise, we use \verb+p+). Finally, % if the third argument is provided, we use it as a label. +% Notice that, if the grammar is not a float (is inline), we do \emph{not} +% break line before and after the grammar. % \begin{macrocode} \ExplSyntaxOn %% 1st argument: caption (makes it float) %% 2nd argument: positionning option (`p` by default) %% 3rd argument: label - \NewDocumentEnvironment{grammar} {o o o} + \NewDocumentEnvironment{grammar} {o O{p} o} { - \IfNoValueTF{#1} { - }{ - \IfNoValueTF{#2}{ - \begin{floatgrammar}[p] - } { - \begin{floatgrammar}[#2] - } + \IfNoValueF{#1}{ + \begin{floatgrammar}[#2] \centering } - \begin{tabular}{=l +r +l +l} + \begin{tabular}{\formal@reset l \formal@add r \formal@add l \formal@add l} }{ \end{tabular} - \IfNoValueTF{#1} { - \linebreak - } { + \IfNoValueF{#1}{ \caption{#1} - \IfNoValueTF{#3}{ - } { + \IfNoValueF{#3}{ \label{#3} } \end{floatgrammar} @@ -316,7 +327,7 @@ % cell is the rule (it is directly printed, without any modification), and the % last cell is the description of the rule, in greyish color. % \begin{macrocode} -\newcommand{\firstcase}[3]{\(\mathcal{#1}\) & \(::=\) & \(#2\) & {\itshape \color{gray!90!black} #3}\\} +\newcommand{\firstcase}[3]{\(\mathcal{#1}\) & \(\formal@Coloneqq\) & \(#2\) & {\itshape \color{gray!90!black} #3}\\} % \end{macrocode} % \end{macro} % @@ -324,7 +335,7 @@ % The |\firstcasesubtil| is implemented similarly to |\firstcase|, except that % the first argument is not surrounded by \verb+\mathcal+. % \begin{macrocode} -\newcommand{\firstcasesubtil}[3]{#1 & \(::=\) & \(#2\) & {\itshape \color{gray!90!black} #3}\\} +\newcommand{\firstcasesubtil}[3]{#1 & \(\formal@Coloneqq\) & \(#2\) & {\itshape \color{gray!90!black} #3}\\} % \end{macrocode} % \end{macro} @@ -379,6 +390,18 @@ % \end{macrocode} % \end{macro} % +% Finally, we check, at the end of the preamble, if there already exist a +% \verb+::=+ symbol. We search for a command called \verb+Coloneqq+, e.g. +% defined in the \href{https://www.ctan.org/pkg/mathtools}{mathtools}. +% \begin{macrocode} +\AtBeginDocument{% + \ifdefined\Coloneqq + \let\formal@Coloneqq\Coloneqq + \else + \newcommand{\formal@Coloneqq}{::=} + \fi +} +% \end{macrocode} % % \Finale \endinput diff --git a/Master/texmf-dist/tex/latex/formal-grammar/formal-grammar.sty b/Master/texmf-dist/tex/latex/formal-grammar/formal-grammar.sty index 90eaa407da6..990b3e5a4d3 100644 --- a/Master/texmf-dist/tex/latex/formal-grammar/formal-grammar.sty +++ b/Master/texmf-dist/tex/latex/formal-grammar/formal-grammar.sty @@ -20,9 +20,9 @@ %% \NeedsTeXFormat{LaTeX2e} -\ProvidesPackage{formal-grammar}[2021/11/10 v1.0 A package to typeset formal grammars] +\ProvidesPackage{formal-grammar}[2021/11/15 v1.1 A package to typeset formal grammars] \RequirePackage{xparse} -\RequirePackage{float} +\RequirePackage{newfloat} \RequirePackage{xcolor} \RequirePackage{colortbl} \RequirePackage{array} @@ -30,49 +30,44 @@ %% \begin{abstract} %% The \textsf{formal-grammar} package provides commands to typeset formal grammars. %% \end{abstract} -\newfloat{floatgrammar}{t b h p}{.gram} -\floatname{floatgrammar}{Grammar} +\DeclareFloatingEnvironment[ +name=Grammar, +listname={List of Grammars}, +placement=tbhp, +]{floatgrammar} %% Taken from https://tex.stackexchange.com/a/26364/107341 -\newcommand*{\@rowstyle}{} +\newcommand*{\formal@rowstyle}{} \newcommand*{\rowstyle}[1]{% sets the style of the next row - \gdef\@rowstyle{#1}% - \@rowstyle\ignorespaces% + \gdef\formal@rowstyle{#1}% + \formal@rowstyle\ignorespaces% } -\newcolumntype{=}{% resets the row style - >{\gdef\@rowstyle{}}% +\newcolumntype{\formal@reset}{% resets the row style + >{\gdef\formal@rowstyle{}}% } -\newcolumntype{+}{% adds the current row style to the next column - >{\@rowstyle}% +\newcolumntype{\formal@add}{% adds the current row style to the next column + >{\formal@rowstyle}% } %% End of stackexchange \ExplSyntaxOn %% 1st argument: caption (makes it float) %% 2nd argument: positionning option (`p` by default) %% 3rd argument: label -\NewDocumentEnvironment{grammar} {o o o} +\NewDocumentEnvironment{grammar} {o O{p} o} { -\IfNoValueTF{#1} { -}{ -\IfNoValueTF{#2}{ -\begin{floatgrammar}[p] -} { +\IfNoValueF{#1}{ \begin{floatgrammar}[#2] -} \centering } -\begin{tabular}{=l +r +l +l} +\begin{tabular}{\formal@reset l \formal@add r \formal@add l \formal@add l} }{ \end{tabular} -\IfNoValueTF{#1} { -\linebreak -} { +\IfNoValueF{#1}{ \caption{#1} -\IfNoValueTF{#3}{ -} { +\IfNoValueF{#3}{ \label{#3} } \end{floatgrammar} @@ -80,8 +75,8 @@ } \ExplSyntaxOff -\newcommand{\firstcase}[3]{\(\mathcal{#1}\) & \(::=\) & \(#2\) & {\itshape \color{gray!90!black} #3}\\} -\newcommand{\firstcasesubtil}[3]{#1 & \(::=\) & \(#2\) & {\itshape \color{gray!90!black} #3}\\} +\newcommand{\firstcase}[3]{\(\mathcal{#1}\) & \(\formal@Coloneqq\) & \(#2\) & {\itshape \color{gray!90!black} #3}\\} +\newcommand{\firstcasesubtil}[3]{#1 & \(\formal@Coloneqq\) & \(#2\) & {\itshape \color{gray!90!black} #3}\\} \newcommand{\otherform}[2]{& \(|\) & \(#1\) & {\itshape \color{gray!90!black} #2}\\} @@ -96,6 +91,13 @@ \newcommand{\lochighlight}[1]{{\color{red} #1}} \newcommand{\downplay}[0]{\rowstyle{\color{white!80!black}}} +\AtBeginDocument{% +\ifdefined\Coloneqq +\let\formal@Coloneqq\Coloneqq +\else +\newcommand{\formal@Coloneqq}{::=} +\fi +} \endinput %% %% End of file `formal-grammar.sty'. -- cgit v1.2.3