3

I am making some elaborate truth tables, and I want to be able to define the further columns in terms of the initial few atomic variables, so that I don't manually have to update the whole thing for each row (of which there are at least ten). At the moment, here's what my approach was:

\newcommand{\andand}[2]
  {
    \ifthenelse{\equal{#1}{#2}}{\ifthenelse{\equal{#1}{1}}{1}{0}}{0}
  }
  \newcommand{\oror}[2]
  {
    \ifthenelse{\equal{#1}{1}}{1}{\ifthenelse{\equal{#2}{1}}{1}{0}}
  }
\newcommand\pp{1}
\newcommand\qq{1}
\newcommand\rr{1}

These are two commands that take in arguments as 1s and/or 0s and logically combine them as AND or OR.

Here's what the first row of my table looks like:

\renewcommand\pp{0}\pp        &
\renewcommand\qq{0}\qq        &
\renewcommand\rr{0}\rr        &
\andand{\pp}{\qq}             &    
\andand{\pp}{\rr}             & 
\oror{\qq}{\rr}               &  
\andand{\pp}{\oror{\qq}{\rr}} &
\oror{\andand{\pp}{\qq}}{\andand{\pp}{\rr}}    \\\hline

Now, the first six lines work just fine: I am redefining variables p,q,r to assume whatever values I want per that row. In the cells that follow, I am actually using the variables to evaluate logical statements. However, in the final two lines, when I try to evaluate the output of an existing function, it refuses to do that and throws an error.

I would like to be able to do this, perhaps using something other than \newcommand if it's not possible to do it in this manner. As an extension, I would also like to be able to programmatically iterate over all combinations of p,q,r (000,001,010,...), and output this as a table.

3
  • Welcome! Could you please edit your question to make your code compilable? That's much more helpful than mere fragments.
    – cfr
    Commented Jan 31, 2018 at 1:13
  • You know there are simple scripts which take a wff and output .tex code for the corresponding truth table? This is not self-contained, but it is very nice and something like python can do this much faster and more simply than anything TeX based. (Probably if you use LuaTeX you could incorporate a lua script into the compilation, but I don't know enough about the Lua available in LuaTeX to have a clue.)
    – cfr
    Commented Jan 31, 2018 at 1:16
  • 1
    LaTeX for Logicians recommends mrieppel.net/prog/truthtable.html, for example. I've also used a python script I could download and use locally, so there are several options.
    – cfr
    Commented Jan 31, 2018 at 1:23

1 Answer 1

2

Here's one approach that uses the pgf utilities \foreach and \pgfmathparse but is unfortunately quite counter-intuitive. The basic idea is that \pgfmathparse can evaluate logical expressions input in a human-readable way and \foreach can iterate over values. These work great together.

The problems arise when you need to put the output into a table. \foreach operates within a group so that the loop variables don't leak into global scope. However, the commands that control table layout won't work inside a group. The solution is to build the table in a macro and expand it later. Unfortunately, this approach leads to a few subtleties related to expansion and to catcodes. Anyways here's the code:

\documentclass{article}
\usepackage{pgfmath}
\usepackage{pgffor}

\begin{document}
\makeatletter
\def\xaddtomacro#1#2{%
    \protected@xdef#1{\unexpanded\expandafter{#1}#2}%
}
\protected\def\printmathresult#1{\scantokens{\pgfmathparse{int(#1)}}\pgfmathresult}
\makeatother

\let\temp\empty

\foreach \p in {0,1}
\foreach \q in {0,1}
\foreach \r in {0,1}{%
    \xaddtomacro\temp{%
      \p &  \q &  \r &
      \printmathresult{\p && \q} &
      \printmathresult{\p && \r} &
      \printmathresult{\q || \r} &
      \printmathresult{\p && ( \q || \r) }  &
      \printmathresult{(\p && \q) ||  ( \p && \r) }  
      \\
    }%
}

\begin{tabular}{cccccccc}
p & q & r & \\\hline
\temp
\end{tabular}
\end{document} 

truth table (needs headers, obviously)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .