summaryrefslogtreecommitdiff
path: root/macros
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2021-10-03 03:01:24 +0000
committerNorbert Preining <norbert@preining.info>2021-10-03 03:01:24 +0000
commitc7bf3f5a728fcb426d1aa38fbec211d4344a3725 (patch)
tree87fbde2d0e79f9e5fce12cbb71971717568fbf9d /macros
parentac5a96ddb02c62d71eb214ed35e3be9a154d991f (diff)
CTAN sync 202110030301
Diffstat (limited to 'macros')
-rw-r--r--macros/luatex/latex/truthtable/README.md26
-rw-r--r--macros/luatex/latex/truthtable/res/exampletable.tex9
-rw-r--r--macros/luatex/latex/truthtable/res/exampletableoutput.tex5
-rw-r--r--macros/luatex/latex/truthtable/truthtable.pdfbin0 -> 106754 bytes
-rw-r--r--macros/luatex/latex/truthtable/truthtable.sty148
-rw-r--r--macros/luatex/latex/truthtable/truthtable.tex192
6 files changed, 380 insertions, 0 deletions
diff --git a/macros/luatex/latex/truthtable/README.md b/macros/luatex/latex/truthtable/README.md
new file mode 100644
index 0000000000..18baedb858
--- /dev/null
+++ b/macros/luatex/latex/truthtable/README.md
@@ -0,0 +1,26 @@
+# truthtable
+
+*Copyright (C) Dominic Flück (K-Trout) 2021* <k-trout.ctan[at]protonmail[dot]ch>
+
+truthtable 2021/10/01 v0.0.1
+
+---
+
+The package is distributed under the **LaTeX Public Project License v1.3c** or later.
+
+Details: [www.latex-project.org/lppl.txt](https://www.latex-project.org/lppl.txt)
+
+---
+
+## Info
+
+A small LaTeX package for generating truth tables automatically.
+
+## Provided files
+
+- README.md (This file)
+- [truthtable.sty](truthtable.sty) The package file
+- [truthtable.pdf](truthtable.pdf) The compiled package documentation
+- [truthtable.tex](truthtable.tex) The package documentation file
+- [res/exampletable.tex](res/exampletable.tex) Sample usage code
+- [res/exampletableoutput.tex](res/exampletableoutput.tex) Sample usage code output \ No newline at end of file
diff --git a/macros/luatex/latex/truthtable/res/exampletable.tex b/macros/luatex/latex/truthtable/res/exampletable.tex
new file mode 100644
index 0000000000..aabfc488c3
--- /dev/null
+++ b/macros/luatex/latex/truthtable/res/exampletable.tex
@@ -0,0 +1,9 @@
+\begin{table}[h]
+ \centering
+ \begin{tabular}{c|c||c|c|c|c|c}
+
+ % Content of table is generated using this single command.
+ \truthtable{A,B}{$A$,$B$}{!A, A & B, A | B, >>(A; B), __(A; B)}{$\lnot A$, $A \land B$, $A \lor B$, $A \rightarrow B$, $A \leftrightarrow B$}{$T$}{$F$}
+
+ \end{tabular}
+\end{table} \ No newline at end of file
diff --git a/macros/luatex/latex/truthtable/res/exampletableoutput.tex b/macros/luatex/latex/truthtable/res/exampletableoutput.tex
new file mode 100644
index 0000000000..507049ca14
--- /dev/null
+++ b/macros/luatex/latex/truthtable/res/exampletableoutput.tex
@@ -0,0 +1,5 @@
+$A$ & $B$ & $\lnot A$ & $A \land B$ & $A \lor B$ & $A \rightarrow B$ & $A \leftrightarrow B$ \\ \hline
+$T$ & $T$ & $F$ & $T$ & $T$ & $T$ & $T$ \\
+$T$ & $F$ & $F$ & $F$ & $T$ & $F$ & $F$ \\
+$F$ & $T$ & $T$ & $F$ & $T$ & $T$ & $F$ \\
+$F$ & $F$ & $T$ & $F$ & $F$ & $T$ & $T$ \\ \ No newline at end of file
diff --git a/macros/luatex/latex/truthtable/truthtable.pdf b/macros/luatex/latex/truthtable/truthtable.pdf
new file mode 100644
index 0000000000..bd2c8df7d4
--- /dev/null
+++ b/macros/luatex/latex/truthtable/truthtable.pdf
Binary files differ
diff --git a/macros/luatex/latex/truthtable/truthtable.sty b/macros/luatex/latex/truthtable/truthtable.sty
new file mode 100644
index 0000000000..94403911ad
--- /dev/null
+++ b/macros/luatex/latex/truthtable/truthtable.sty
@@ -0,0 +1,148 @@
+% truthtable.sty
+%% Copyright 2021 D. Flück
+%
+% This work may be distributed and/or modified under the
+% conditions of the LaTeX Project Public License, either version 1.3
+% of this license or (at your option) any later version.
+% The latest version of this license is in
+% http://www.latex-project.org/lppl.txt
+% and version 1.3 or later is part of all distributions of LaTeX
+% version 2005/12/01 or later.
+%
+% This work has the LPPL maintenance status “author-maintained”.
+%
+% The Current Maintainer of this work is D. Flück.
+%
+% This work consists of the file truthtable.sty.
+\NeedsTeXFormat{LaTeX2e}[1994/06/01]
+\ProvidesPackage{truthtable}[2021/10/01 0.0.1 Package for generating truth tables automatically using LuaTeX]
+
+\ProcessOptions\relax
+\@ifpackageloaded{luacode}{
+ \PackageWarningNoLine{truthtable}{Package luacode was already loaded}
+}{
+ \RequirePackage{luacode}
+}
+
+\begin{luacode*}
+
+function Impl(a,b)
+ return (not a or b);
+end
+
+function Equiv(a,b)
+ return ((a and b) or ((not a) and (not b)));
+end
+
+function ComputeRows(header)
+ return 2^header
+end
+
+function Split(s, delimiter)
+ local result = {};
+ for match in (s..delimiter):gmatch("(.-)"..delimiter) do
+ table.insert(result, match);
+ end
+ return result;
+end
+
+function EvaluateFormula(formula)
+ local parsedFormula = "function res() return( " .. string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(string.gsub(formula, " ", ""),">>","Impl"),"__","Equiv"),"!","not "),"&" ," and "),"|"," or "),";",",") .. " ) end";
+
+ chunk = load(parsedFormula);
+ chunk();
+ local result = res();
+ return result;
+end
+
+function toBits(num)
+ local t = "" -- will contain the bits
+ while num>0 do
+ local rest = math.fmod(num,2)
+ if (rest == 1) then
+ t = "1" .. t
+ else
+ t = "0" .. t
+ end
+
+ num=(num-rest)/2
+ end
+ return t;
+end
+
+function printTruthValue(expr, dTrue, dFalse)
+
+ local returnVal = ""
+
+ if (expr) then
+ returnVal = dTrue;
+ else
+ returnVal = dFalse;
+ end
+
+ return returnVal;
+end
+
+function parse(commaSepVariables, commaSepDisplayVariables, commaSepResultRows, commaSepResultDisplayRows, displayTrue, displayFalse)
+ local vrbls = Split(commaSepVariables, ",");
+ local numberOfColumns = #(vrbls);
+ local rows = ComputeRows(numberOfColumns);
+ local dVrbls = Split(commaSepDisplayVariables, ",");
+ local resRows = Split(commaSepResultRows, ",");
+ local dResRows = Split(commaSepResultDisplayRows, ",");
+
+ local dHeader = string.gsub(commaSepDisplayVariables, ",", " & ") .. " & " .. string.gsub(commaSepResultDisplayRows, ",", " & ") .. " \\\\ \\hline";
+
+ if (not (numberOfColumns == #(vrbls) and numberOfColumns == #(dVrbls))) then
+ return("error");
+ end
+
+ local tableContent = dHeader;
+
+ for i = (rows - 1),0,-1
+ do
+ local bitString = toBits(i);
+
+ while #bitString < numberOfColumns do
+ bitString = "0" .. bitString
+ end
+
+ local wVrbls = commaSepVariables;
+ local wCommaSepRows = commaSepResultRows
+ for ii = 1,numberOfColumns
+ do
+ wVrbls = string.gsub(wVrbls, vrbls[ii], (string.sub(bitString,ii,ii) == "1" ) and "+" or "-" )
+ wCommaSepRows = string.gsub(wCommaSepRows, vrbls[ii], (string.sub(bitString,ii,ii) == "1" ) and "+" or "-" )
+ end
+
+ local aWVrbls = Split(string.gsub(string.gsub(wVrbls, "+", "true"),"-", "false"), ",");
+
+ local aWCommaSepRows = Split(string.gsub(string.gsub(wCommaSepRows, "+", "true"),"-", "false"), ",");
+
+ local row = "";
+
+ for c = 1,#(aWVrbls)
+ do
+ row = row .. printTruthValue(EvaluateFormula(aWVrbls[c]), displayTrue, displayFalse) .. " & ";
+ end
+
+ for c = 1,#(aWCommaSepRows)
+ do
+ row = row .. printTruthValue(EvaluateFormula(aWCommaSepRows[c]), displayTrue, displayFalse) .. " & ";
+ end
+
+ row = string.sub(row, 1, #row - 2) .. "\\\\"
+
+ tableContent = tableContent .. "\n" .. row
+ end
+
+ tex.print(tableContent);
+end
+
+\end{luacode*}
+
+\newcommand{\truthtable}[6]{
+ \luadirect{parse("#1", "\luaescapestring{#2}", "\luaescapestring{#3}", "\luaescapestring{#4}", "\luaescapestring{#5}","\luaescapestring{#6}")}
+}
+
+\endinput \ No newline at end of file
diff --git a/macros/luatex/latex/truthtable/truthtable.tex b/macros/luatex/latex/truthtable/truthtable.tex
new file mode 100644
index 0000000000..a6d62fc606
--- /dev/null
+++ b/macros/luatex/latex/truthtable/truthtable.tex
@@ -0,0 +1,192 @@
+\documentclass[a4paper]{article}
+\usepackage[in]{fullpage}
+
+\usepackage{listings}
+\usepackage{xcolor}
+\usepackage{caption}
+\lstset{
+ basicstyle=\footnotesize\ttfamily, % Default font
+ numbers=none, % left % Location of line numbers
+ numberstyle=\tiny, % Style of line numbers
+ % stepnumber=2, % Margin between line numbers
+ numbersep=0pt, %5pt, % Margin between line numbers and text
+ tabsize=1, % Size of tabs
+ extendedchars=true,
+ breaklines=true, % Lines will be wrapped
+ % keywordstyle=\color{red}\textbf,
+ commentstyle=\color{purple},
+ identifierstyle=\color{blue},
+ frame=tlrb,
+ keywordstyle={[1]\color{red}\textbf},
+ keywordstyle={[2]\color{blue}\textbf},
+ keywordstyle={[3]\color{green}},
+ keywordstyle={[4]\textbf},
+ stringstyle=\color{purple}\ttfamily, % Color of strings
+ showspaces=false,
+ showtabs=false,
+ xleftmargin=5.5pt,%17pt,
+ framexleftmargin=2pt,%17pt,
+ framexrightmargin=2.5pt,
+ framexbottommargin=2pt,
+ backgroundcolor=\color[HTML]{E6E7E7},
+ showstringspaces=false,
+}
+\lstloadlanguages{ % Check documentation for further languages ...
+ [LaTeX]TeX
+}
+\DeclareCaptionFont{blue}{\color{blue}}
+
+\captionsetup[lstlisting]{singlelinecheck=false, labelfont={blue}, textfont={blue}}
+
+\DeclareCaptionFont{white}{\color{white}}
+\DeclareCaptionFormat{listing}{\colorbox[cmyk]{1, 0.33, 0,0.4}{\parbox{\textwidth}{\hspace{3pt}#1#2#3}}}
+\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white, singlelinecheck=false, margin=0pt, font={bf,footnotesize}}
+
+\usepackage[colorlinks]{hyperref}
+\hypersetup{
+ bookmarksopen=true,
+ bookmarksnumbered=true,
+ pdftitle={truthtable},
+ pdfauthor={Dominic Flück <K-Trout>},
+ pdfsubject={truth LaTeX package},
+ pdfkeywords={truthtable,LuaLaTeX, package, documentation, LaTeX},
+ pdfdisplaydoctitle=true,
+ pdflang={en-GB},
+ pdfcreator={LuaHBTeX},
+ pdfproducer={LuaHBTeX, Version 1.13.2}}
+
+\usepackage{truthtable}
+
+\usepackage{bxtexlogo}
+\usepackage[ukenglish]{babel}
+
+\title{\texttt{truthtable}\textbf{: \LaTeX\ Package\\for automatically generated Truth Tables}}
+\author{Dominic Flück <K-Trout>}
+\date{0.0.1 2021/10/01}
+
+\begin{document}
+ \maketitle
+
+ \begin{abstract}
+ \noindent \texttt{truthtable} is a \LaTeX\ package for creating automatically generating truth tables given a table header. It supports a number of logical operations which can be combined as needed. It's built upon the package \href{https://ctan.org/pkg/luacode}{\texttt{luacode}} and therefor has to be used with the \LuaLaTeX\ compiler.
+ \end{abstract}
+
+ \tableofcontents
+
+ \section{Introduction}
+ Tables in \LaTeX\ have the reputation of being a bit tedious. When creating a table with many cells, such as a truth table, they are not only tedious to build, but also not very readable.
+
+ To help this situation when creating a truth table for a document, this package provides a macro, which allows simply for the variables and the columns of a truth table to be defined. The package then takes care of the rest.
+
+ \section{Dependencies}
+
+ \texttt{truthtable} uses the package \href{https://ctan.org/pkg/luacode}{\texttt{luacode}} to run, as the heavy lifting of the processing is done in \emph{Lua}. The package checks if \href{https://ctan.org/pkg/luacode}{\texttt{luacode}} is already loaded, and if not, does so. \LuaLaTeX is required to compile the resulting documents.
+
+ \section{Usage}
+
+ The \texttt{truthtable} package provides as of this version a single command:\\ \texttt{\textbackslash truthtable\{\emph{comma separated variables}\}\{\emph{comma separated display variables}\}\\\{\emph{comma separated statements}\}\{\emph{comma separated display statements}\}\\\{\emph{display true value}\}\{\emph{display false value}\}}
+
+ This command is placed in the normal table boilerplate. This leads to the redundant practice of defining the column count twice, once for the table environment as the column layout and once in the command by defining the variables and statements.\footnote{See \autoref{lst:exampletable} for example}
+
+ This is intentional to allow for more flexibility in customising the column layout as well as pre- and appending of further rows to the table.
+
+ \subsection{Comma separated variables}
+
+ The basic variables, for which every combination of \emph{true} and \emph{false} a row of table will be generated. The variables should be relatively simple, as they are not used for the formatting the table but simply to calculate the answers. The variables should be separated using commas. Don't use variables, which contain another variable, i.e., don't do this: \emph{\{n,An\}}.
+
+ \subsection{Comma separated display variables}
+
+ These are the display values corresponding to the \emph{Comma separated variables}. Fancy variable formatting can be applied. At least normal text and math mode seem to work.\footnote{More testing needs to be done}
+ The same number of display variables as variables is required. The comma cannot be used as a display character, as it is used as delimiter.
+
+ \subsection{Comma separated statements}
+
+ The statements using the \emph{Comma separated variables} which are used to evaluate the statements for any given combination of variables. Parentheses can be used in the normal fashion to indicate the order of combined statements. The notation for the different operations is as follows:
+
+ \subsubsection{\texttt{NOT} / Negation}
+
+ To negate a variable or statement, the exclamation point \texttt{!} is used.
+ \begin{itemize}
+ \item $\lnot A$: \texttt{!A}
+ \item $\lnot (\lnot A)$: \texttt{!(!A)}
+ \end{itemize}
+
+ \subsubsection{\texttt{AND} / Conjunction}
+
+ For the conjunction of two variables or statements the and symbol \texttt{\&} is used. \textbf{The \& must not be escaped for the comma separated statements!}
+ \begin{itemize}
+ \item $A \land B$: \texttt{A \& B}
+ \item $A \land (A \land B)$: \texttt{A \& (A \& B)}
+ \end{itemize}
+
+ \subsubsection{\texttt{OR} / Disjunction}
+
+ For the Disjunction of two variables or statements the vertical line character \texttt{|} is used.
+
+ \begin{itemize}
+ \item $A \lor B$: \texttt{A | B}
+ \item $A \lor (A \lor B)$: \texttt{A | (A | B)}
+ \end{itemize}
+
+ \subsubsection{$\rightarrow$ / Implication}
+
+ The implication can also be expressed. \textbf{Note that the delimiter used is the semicolon \texttt{;} and not the comma \texttt{;} ! This is because the statements are separated using the comma.}
+
+ \begin{itemize}
+ \item $A \rightarrow B$: \texttt{>>(A; B)}
+ \item $A \rightarrow (A \lor B)$: \texttt{>>(A; (A | B))}
+ \item $A \land (A \rightarrow B)$: \texttt{A \& >>(A; B)}
+ \end{itemize}
+
+ \subsubsection{$\leftrightarrow$ / Equality}
+
+ The equality can also be expressed. \textbf{Note that the delimiter used is the semicolon \texttt{;} and not the comma \texttt{;} ! This is because the statements are separated using the comma.} \textbf{The \texttt{\_\_} must not be escaped for the comma separated statements!}
+
+ \begin{itemize}
+ \item $A \leftrightarrow B$: \texttt{\_\_(A; B)}
+ \item $A \leftrightarrow (A \lor B)$: \texttt{\_\_(A; (A | B))}
+ \item $A \land (A \leftrightarrow B)$: \texttt{A \& \_\_(A; B)}
+ \end{itemize}
+
+ \subsection{Comma separated display statements}
+
+ Display statements are defined the same way as the \emph{comma separated display variables}. The comma cannot be used as a display character, as it is used as delimiter.
+
+ \subsection{Display true value}
+
+ The displaying string which will be used in the table body for \emph{true}.Normal text and math mode can be used.
+
+ \subsection{Display false value}
+
+ The displaying string which will be used in the table body for \emph{false}.Normal text and math mode can be used.
+
+ \newpage
+
+ \section{Example of use}
+
+ The code snippet seen in \autoref{lst:exampletable} is the entirety of code required to produce the truth table seen in \autoref{tab:sampletable}.\footnote{The captioning setup was omitted in the listing.}
+
+ The command generates the code seen in \autoref{lst:exampletableoutput}.
+
+ \lstinputlisting[language={[LaTeX]TeX}, label=lst:exampletable, caption=Code for an sample truth table]{res/exampletable.tex}
+
+ \lstinputlisting[language={[LaTeX]TeX}, label=lst:exampletableoutput, caption=Code generated by \textbackslash truthtable]{res/exampletableoutput.tex}
+
+ \begin{table}[h]
+ \centering
+ \begin{tabular}{c|c||c|c|c|c|c}
+ \truthtable{A,B}{$A$,$B$}{!A, A & B, A | B, >>(A; B), __(A; B)}{$\lnot A$, $A \land B$, $A \lor B$, $A \rightarrow B$, $A \leftrightarrow B$}{$T$}{$F$}
+ \end{tabular}
+ \caption{Sample truth table}
+ \label{tab:sampletable}
+ \end{table}
+
+ \section{Known issues and bugs}
+
+ \begin{description}
+ \item[Stability] The Lua code of the macro is not yet error resistant. Either a table is produced or not, warnings and errors are not yet implemented. Future versions of the package will probably address this.
+ \item[Display formatting] Whilst normal text and math mode work for both headers and truth values, other text formatting such as \texttt{\textbackslash textbf} does not. It is not yet clear if this will be addressed in future versions.
+ \item[Operations] For the moment only four operations are defined. Further operations\footnote{\emph{NAND}, \emph{XOR}, \emph{etc.}} may be added in future versions.
+ \end{description}
+
+\end{document} \ No newline at end of file