# Truth table generation giving weird math mode error?

I'm trying to write a truth table in LaTeX so that it's only takes up as much space as needed. I got the idea to do this with the forest environment when I created truth trees (semantic tableau) using the same approach. However, I'm running into an issue where LaTeX (TexStudio in particular) says that the mathrm command is only available in math mode, when it's already in math mode...

I'm sure what I'm doing isn't really good practice, so maybe someone here can see what I'm doing wrong. Here's my code:

\documentclass[tikz,border=10pt]{standalone}
\newcommand*\lif{\mathbin{\to}}% added thanks to egreg's suggestion
\usepackage{forest}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{array}
\newcommand{\varlnot}{\mathord{\sim}}
\newcommand{\varland}{\mathbin{\&}}
\newcommand{\varliff}{\leftrightarrow}
\newcommand{\dneg}{\varlnot\varlnot}
\newcolumntype{C}{>$c<$}
\begin{document}
\begin{forest}
[
$\begin{array}{C|C|C|C} \mathrm{A} & \mathrm{S} & (\mathrm{A} \varliff \mathrm{S}) & \varlnot (\mathrm{A} \varliff \mathrm{S})\\ \hline true & true & true & false \\ true & false & false & true \\ false & true & false & true \\ false & false & true & false \\ \end{array}$
]
\end{forest}
\end{document}


Expected (and given!) output from Overleaf.

I am really confused so any help would be greatly appreciated!

• Can you please elaborate on why you think that you need the forest package for a truth table? – Dr. Manuel Kuehner Apr 17 at 20:30
• @Dr.ManuelKuehner Well, I guess I really don't need it per-se. What I found out, though, is that if I use it, I can condense the truth table to take up only as much space as needed (crappy workaround I'm certain). If there's another way to accomplish what I need I'd love to hear it! – Joshua Crotts Apr 17 at 20:32
• your error is using C columns (so forcing text mode) with math constructs such as \mathrm (forest is doing nothing useful here) – David Carlisle Apr 17 at 20:53

• This is perfect (and what I was wondering about). Thanks! – Joshua Crotts Apr 17 at 21:09
• @Dr.ManuelKuehner yes exactly egreg gets most of his answers from my comments:-) – David Carlisle Apr 18 at 6:31
• @Dr.ManuelKuehner The comment may have appeared while I was writing my answer. If you try $$x\times y$$ in a cell of a tabular you will get an error. Not because display math mode is not allowed, but because cells are typeset in restricted horizontal mode, where display math mode does not even exist and  only produces an empty math formula. – egreg Apr 18 at 7:48
• Thanks, both of you :). I did by no means had any negative element in my question :). – Dr. Manuel Kuehner Apr 18 at 15:48
• I assume that I probably misunderstand your question.
• Anyway, here's a simple table with upright variables (A, B) in math mode using \text{} from the amsmath package (mathtools loads amsmath).

\documentclass{article}

\usepackage{mathtools}
\usepackage{booktabs}

\begin{document}

% Alternatives for "not" are \neg and \lnot (instead of \sim).

\begin{table}
\begin{tabular}{@{}llll@{}}
\toprule
A     & B     & $(\text{A}\leftrightarrow\text{B})$  & $\sim(\text{A}\leftrightarrow\text{B})$  \\ \midrule
True  & True  & True  & False \\
True  & False & False & True  \\
False & True  & False & True  \\
False & False & True  & False \\ \bottomrule
\end{tabular}
\end{table}

\end{document}


## Update 1

I added float package that offers the [H] placement option and I added a table that fills the complete text width (tabularx package).

\documentclass{article}

\usepackage{mathtools}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{showframe}
\usepackage{float}

\begin{document}

\begin{table}[H]
\begin{tabular}{@{}llll@{}}
\toprule
A     & B     & $(\text{A}\leftrightarrow\text{B})$  & $\sim(\text{A}\leftrightarrow\text{B})$  \\ \midrule
True  & True  & True  & False \\
True  & False & False & True  \\
False & True  & False & True  \\
False & False & True  & False \\ \bottomrule
\end{tabular}
\end{table}

\begin{table}[H]
\centering % <-- Centers the table horizontally!
\begin{tabular}{@{}llll@{}}
\toprule
A     & B     & $(\text{A}\leftrightarrow\text{B})$  & $\sim(\text{A}\leftrightarrow\text{B})$  \\ \midrule
True  & True  & True  & False \\
True  & False & False & True  \\
False & True  & False & True  \\
False & False & True  & False \\ \bottomrule
\end{tabular}
\end{table}

\begin{table}[H]
\centering % <-- Not needed here since the table has textwidth.
\begin{tabularx}{\textwidth}{ X  X  X  X}
\toprule
A     & B     & $(\text{A}\leftrightarrow\text{B})$  & $\sim(\text{A}\leftrightarrow\text{B})$  \\ \midrule
True  & True  & True  & False \\
True  & False & False & True  \\
False & True  & False & True  \\
False & False & True  & False \\ \bottomrule
\end{tabularx}
\end{table}

\end{document}


• So, the problem with this version is that the table only takes up a small portion of the page. I want it to take up the entire page instead of being placed arbitrarily in the middle. If you paste my code into Overleaf, you'll see what I mean by taking up only as much space as it needs. – Joshua Crotts Apr 17 at 20:48
• @JoshuaCrotts just remove the begin{table} and \end{table} if you do not want a float – David Carlisle Apr 17 at 20:52
• @JoshuaCrotts, now is not clear, what is your problem. In question you require to write table, which has minimal size, now you requite that it should occupy whole page. Do I misunderstand you? What is your problem? – Zarko Apr 17 at 20:55
• +1 for nice answer! – Zarko Apr 17 at 20:56