\documentclass{article} \usepackage{listings,color,booktabs,longtable,array, hyperref,multirow,multicol,luatruthtable, enumitem} \usepackage[top=1.1in, bottom=1.1in, left=1in, right=1in]{geometry} \renewcommand{\arraystretch}{1.2} \hypersetup{colorlinks,urlcolor=blue} \lstset{frame=none, language=[LaTeX]{TeX}, aboveskip=3mm, belowskip=3mm, showstringspaces=false, columns=flexible, basicstyle={\ttfamily}, numbers=none, numberstyle=\tiny\color{gray}, stringstyle=\color{mauve}, breaklines=true, breakatwhitespace=true, tabsize=1 } \usepackage[backend=bibtex]{biblatex} \addbibresource{luatruthtable} \begin{document} \title{The luatruthtable Package} \author{Chetan Shirore\thanks{Email id: mathsbeauty@gmail.com} \space and Ajit Kumar} \maketitle \section{The luatruthtable Package} The \verb|luatruthtable| package is developed to generate Truth Tables of boolean values in LaTeX. It provides an easy way for generating truth tables in LaTeX . There is no need of special environment in the package for generation of Truth Tables. It is written in lua and tex file is to be compiled with LuaLatex engine. The time required for operations is no issue while compiling with LuaLaTeX. There is no need to install lua on users system as tex distributions (TeXLive or MikTeX) come bundled with LuaLaTeX. It is useful for generation of Truth Tables in tex file itself. The package supports nesting of commands for multiple operations. It can be modified or extended by writing custom lua programs. The programming capabilities of Lua are effectively used in the development of pacakage. The other approaches of generating Truth Table in LaTeX get weired \cite{online.latextruthtable} and probably without using Lua it can't be done in an easier way in LaTeX. The \emph{xkeyval} package is used in its development apart from the \emph{luacode*} package. \section{License} The \verb|luatruthtable| package is released under the LaTeX Project Public License v1.3c or later. The complete license text is available at \url{http://www.latex-project.org/lppl.txt}. It is developed in lua. Lua is certified open source software available. Its license is simple and liberal which is compatible with GPL. \section{Installation and Inclusion} The installation of \verb|luatruthtable| package is similar to plain latex package where \texttt{.sty} file is in LaTeX directory of texmf tree. The package can be used by including the command \verb|\luatruthtable| in the preamble of latex document. The tex file is to be compiled using the LuaLaTeX engine. \section{Core Ideas used in the development of the package} The function \verb|toBinary(x,y)| is used to produce sequence of \emph{True} and \emph{False} values of boolean variables. This function converts the decimal number \verb|x| to a binary number by adding \verb|y| number of leading zeroes. The result of this is stored in a table in Lua. Here \verb|y| corresponds to number of boolean variables. As \(2^y\) permutations of boolean variables are to be produced, the function \verb|toBinary(x,y)| runs inside a loop where \verb|x| takes values from \(1\) to \(y\). The splitting of variables and expressions is done using string methods available in Lua. The nested \emph{metamethods} in Lua are used to define several logical operators. The \emph{load} function in Lua is used to evaluate logical expressions. \section{The luaTruthTable command} The \verb|\luaTruthTable| command is the main command in the \emph{luatruthtable} package which generates truth tables. It has the following syntax. \begin{lstlisting}[caption={[]}] \luaTruthTable[trtext,fltext]{list of boolean/logical variables} {list of expressions} \end{lstlisting} The command has two mandatory arguments. \\ i) \verb|list of boolean / logical variables| : The list of boolean or logical variables should be separated by a comma. \\ ii) \verb|list of expressions| : The list of logical expressions that are to be evaluated should be separated by a comma. \\ The command has two optional arguments. \\ i) \verb|trtext| : The \verb|trtext| is the display value for the boolean value \emph{True}. It has the default value \verb|$T$| in the package. It can be any string or number. Assigning value \(0\) should be avoided to \verb|trtext| variable. \\ ii) \verb|fltext| : The \verb|fltext| is the display value for the boolean value \emph{False}. It has the default value \verb|$F$| in the package. It can be any string or number. Assigning value \(1\) should be avoided to \verb|fltext| variable. The \verb|\luaTruthTable| command should be used within in the environment \verb|\begin{tabular} ... \end{tabular}| or any other environment in LaTeX for tables. The sequence of column heads should be same as sequence of \verb|list of boolean/logical variables| and \verb|list of expressions|. Without these correct inputs, the command \verb|\luaTruthTable| can not produce the desired output. \section{Operations in the luatruthtable package} \begin{enumerate}[label=\alph*)] \item \emph{not}: The value of \emph{ not p} is False when \(p\) is True and it is True when \(p\) is False. \begin{center} \begin{tabular}{|c|c|} \hline \(p\) & not \(p\) \\ \hline \luaTruthTable{p}{lognot*p} \\ \hline \end{tabular} \end{center} The command \verb|lognot*| is used in the package to generate truth table for \emph{not} operation. \item \emph{and}: The value of \emph{p AND q} is True if and only if both \(p\) and \(q\) are True. \begin{center} \begin{tabular}{|cc|c|} \hline \(p\) & \(q\) & \(p\) and \(q\) \\ \hline \luaTruthTable{p,q}{p*logand*q} \\ \hline \end{tabular} \end{center} The command \verb|*logand*| is used in the package to generate truth table for \emph{and} operation . \item \emph{or}: The value of \emph{p or q} is False if and only if both \(p\) and \(q\) are False. \begin{center} \begin{tabular}{|cc|c|} \hline \(p\) & \(q\) & \(p\) or \(q\) \\ \hline \luaTruthTable{p,q}{p*logor*q} \\ \hline \end{tabular} \end{center} The command \verb|*logor*| is used in the package to generate truth table for \emph{or} operation. \item \emph{implies}: The value of \emph{p implies q} is False if and only if \(p\) is True and \(q\) is False. \begin{center} \begin{tabular}{|cc|c|} \hline \(p\) & \(q\) & \(p\) implies \(q\) \\ \hline \luaTruthTable{p,q}{p*imp*q} \\ \hline \end{tabular} \end{center} The command \verb|*imp*| is used in the package to generate truth table for \emph{implies} operation. \item \emph{if and only if} : The value of \emph{p if and only if q} is True if and only if both \(p\) and \(q\) have same truth values. \begin{center} \begin{tabular}{|cc|c|} \hline \(p\) & \(q\) & \(p\) iff \(q\) \\ \hline \luaTruthTable{p,q}{p*iff*q} \\ \hline \end{tabular} \end{center} The command \verb|*iff*| is used in the package to generate truth table for \emph{if and only if} operation. \item \emph{NAND} : The value of \emph{p NAND q} is \(0\) if and only if both \(p\) and \(q\) have value \(1\). \begin{center} \begin{tabular}{|cc|c|} \hline \(p\) & \(q\) & \(p\) NAND \(q\) \\ \hline \luaTruthTable[trtext=$1$,fltext=$0$]{p,q}{p*lognand*q} \\ \hline \end{tabular} \end{center} The command \verb|*lognand*| is used in the package to generate truth table for \emph{NAND} operation. \item \emph{XOR} : The value of \emph{p XOR q} is \(0\) if and only if \(p\) and \(q\) have same values. \begin{center} \begin{tabular}{|cc|c|} \hline \(p\) & \(q\) & \(p\) XOR \(q\) \\ \hline \luaTruthTable[trtext=$1$,fltext=$0$]{p,q}{p*logxor*q} \\ \hline \end{tabular} \end{center} The command \verb|*logxor*| is used in the package to generate truth table for \emph{XOR} operation. \item \emph{NOR}: The value of \emph{p NOR q} is \(1\) if and only if both \(p\) and \(q\) have value \(0\). \begin{center} \begin{tabular}{|cc|c|} \hline \(p\) & \(q\) & \(p\) NOR \(q\) \\ \hline \luaTruthTable[trtext=$1$,fltext=$0$]{p,q}{p*lognor*q} \\ \hline \end{tabular} \end{center} The command \verb|*lognor*| is used in the package to generate truth table for \emph{NOR} operation. \item \emph{XNOR}: The value of \emph{p XNOR q} is \(1\) if and only if both \(p\) and \(q\) have same values. \begin{center} \begin{tabular}{|cc|c|} \hline \(p\) & \(q\) & \(p\) XNOR \(q\) \\ \hline \luaTruthTable[trtext=$1$,fltext=$0$]{p,q}{p*logxnor*q} \\ \hline \end{tabular} \end{center} The command \verb|*logxnor*| is used in the package to generate truth table for \emph{XNOR} operation. \end{enumerate} Table \ref{tbl:truthop} describes operators in the \verb|luatruthtable| package. \begin{center} \begin{longtable}{llm{9cm}} \toprule \multicolumn{1}{c}{\textcolor{blue}{Operator}} & \multicolumn{1}{l}{\textcolor{blue}{Syntax}} & \multicolumn{1}{c}{\textcolor{blue}{Description}} \\ \toprule \begin{lstlisting}[caption={[]}] lognot* \end{lstlisting} & \begin{lstlisting}[caption={[]}] lognot*p \end{lstlisting} & Negates the boolean variable \(p\).\\ \midrule \begin{lstlisting}[caption={[]}] *logand* \end{lstlisting} & \begin{lstlisting}[caption={[]}] p*logand*q \end{lstlisting} & Gives the truth value of the expression \emph{p and q}.\\ \midrule \begin{lstlisting}[caption={[]}] *logor* \end{lstlisting} & \begin{lstlisting}[caption={[]}] p*logor*q \end{lstlisting} & Gives the truth value of the expression \emph{p or q}.\\ \midrule \begin{lstlisting}[caption={[]}] *imp* \end{lstlisting} & \begin{lstlisting}[caption={[]}] p*imp*q \end{lstlisting} & Gives the truth value of the expression \emph{if p then q}.\\ \midrule \begin{lstlisting}[caption={[]}] *iff* \end{lstlisting} & \begin{lstlisting}[caption={[]}] p*iff*q \end{lstlisting} & Gives the truth value of the expression \emph{p if and only if q}.\\ \midrule \begin{lstlisting}[caption={[]}] *lognand* \end{lstlisting} & \begin{lstlisting}[caption={[]}] p*lognand*q \end{lstlisting} & Gives the truth value of the expression \emph{ p NAND q}.\\ \midrule \begin{lstlisting}[caption={[]}] *logxor* \end{lstlisting} & \begin{lstlisting}[caption={[]}] p*logxor*q \end{lstlisting} & Gives the truth value of the expression \emph{ p XOR q}.\\ \midrule \begin{lstlisting}[caption={[]}] *lognor* \end{lstlisting} & \begin{lstlisting}[caption={[]}] p*lognor*q \end{lstlisting} & Gives the truth value of the expression \emph{ p NOR q}.\\ \midrule \begin{lstlisting}[caption={[]}] *logxnor* \end{lstlisting} & \begin{lstlisting}[caption={[]}] p*logxnor*q \end{lstlisting} & Gives the truth value of the expression \emph{ p XNOR q}.\\ \bottomrule \\ \caption{Operators in the luatruthtable package} \label{tbl:truthop} \end{longtable} \end{center} \section{Illustrations of commands in the luatruthtable package}\label{illtruth} The \emph{luatruthtable} package can accept a finite number of variables. It supports a finite number of variables that one would need. \begin{lstlisting}[label={code:illluatruth}, caption={LaTeX document with luatruthtable package}] \begin{tabular}{|cc|c|c|c|c|c|c|c|c|c|} \hline \(p\) & \(q\) & \(\neg p\) & \(p \land q\) & \(p\lor q\) & \(p \rightarrow q\) & \(p \leftrightarrow q\) & \( p\) nand \(q\) & \(p\) xor \(q\) & \( p\) nor \(q\) & \(p\) xnor \(q\) \\ \hline \luaTruthTable{p,q}{lognot*p, p*logand*q, p*logor*q, p*imp*q, p*iff*q, p*lognand*q, p*logxor*q, p*lognor*q, p*logxnor*q } \\ \hline \end{tabular} \end{lstlisting} The LaTeX code (Listing \ref{code:illluatruth}) generates the output shown in Table \ref{tbl:luatruthtbl}. \begin{table}[h] \centering \begin{tabular}{|cc|c|c|c|c|c|c|c|c|c|} \hline \(p\) & \(q\) & \(\neg p\) & \(p \land q\) & \(p\lor q\) & \(p \rightarrow q\) & \(p \leftrightarrow q\) & \( p\) nand \(q\) & \(p\) xor \(q\) & \( p\) nor \(q\) & \(p\) xnor \(q\) \\ \hline \luaTruthTable{p,q}{lognot*p, p*logand*q, p*logor*q, p*imp*q, p*iff*q, p*lognand*q, p*logxor*q, p*lognor*q, p*logxnor*q } \\ \hline \end{tabular} \caption{Example 1} \label{tbl:luatruthtbl} \end{table} \begin{lstlisting}[label={code:illluatruth2}, caption={LaTeX document with luatruthtable package}] \begin{tabular}{|cccc|c|} \hline \(p\) & \(q\) & \( r\) & \(s\) & \(((p\land q) \rightarrow \neg r) \land s \) \\ \hline \luaTruthTable{p,q,r,s}{((p*logand*q)*imp*(lognot*r))*logand*s } \\ \hline \end{tabular} \end{lstlisting} The LaTeX code (Listing \ref{code:illluatruth2}) generates the output shown in Table \ref{tbl:luatruthtbl2}. \begin{table}[h] \centering \begin{tabular}{|cccc|c|} \hline \(p\) & \(q\) & \( r\) & \(s\) & \(((p\land q) \rightarrow \neg r) \land s \) \\ \hline \luaTruthTable{p,q,r,s}{((p*logand*q)*imp*(lognot*r))*logand*s } \\ \hline \end{tabular} \caption{Example 2} \label{tbl:luatruthtbl2} \end{table} \section{Known Issues, Limitations and Scope of the package} The associativity and precedence of operator is not yet supported. The package can give appropriate results only when parentheses are used for each of the operation. It gives eraneous result when parentheses are not used. This point is of utmost importance in using the package. These issues are there is no native way of defining custom operations in Lua \cite{online.luacustomoperator}. However some metamethods can be nested in a way to glimpse the operators. All operators defined in this package are instances of such nestings. The question may be raised that is there better way of accomplishing this in Lua. The answer to the point is that there are alternative ways of doing this. They may be better in some or other sense. For example, instead of defining \emph{*logand*} operator and using it in the fashion \emph{p*logand*q}, one could define function \emph{logand} that takes two arguments and use it in a way \emph{logand(p,q)}. But when it comes to embedding it in LaTeX, again one has to use more and more nested parentheses as number of statements and operations increase. This is the exact reason why such approach is not used in the development of package. Instead of using \emph{implies(logand(p,logor(q,r)),s)} it sounds more natural to use \emph{(p *logand* (q*logor*r) )*implies* s}. Apart from these issues, there is no error handling mechanism used in the package. It relies on the error handling of Lua and TeX itself. The package currently supports 9 operations viz. \emph{not,and,nand,or, xor, implies, iff, nor, xnor}. The error handling and extending number of operations may be considered in future versions of the package. \printbibliography \end{document}