3

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.

enter image description here

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

3
  • 2
    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
3

You're in array, so cells are typeset in math mode because each entry is preceded by $ and followed by $. With your C column type, you are essentially typesetting each entry as $$<entry>$$, so no cell is in math mode (here $$ just produces an empty math formula).

To the contrary, if you use >{$}c<{$} in tabular, the cells in that column will be typeset in math mode.

I'm not sure what's the role of forest here.

I'd simply add $ around the headers and use a tabular.

\documentclass[border=10pt]{standalone}
\usepackage{amsmath}
\usepackage{array}

\newcommand{\varlnot}{\mathord{\sim}}
\newcommand{\varland}{\mathbin{\&}}
\newcommand{\varliff}{\leftrightarrow}
\newcommand{\dneg}{\varlnot\varlnot}
\newcommand*\lif{\mathbin{\to}}% added thanks to egreg's suggestion
\newcommand{\ltrue}{\mathrm{true}}

\begin{document}

\begin{tabular}{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{tabular}

\end{document}

enter image description here

5
  • Is David's comment your error is using C columns (so forcing text mode) in alignment with what you are stating? I am confused by the $$ :). – Dr. Manuel Kuehner Apr 17 at 21:05
  • 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
  • 1
    @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
3
  • 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}

enter image description here


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}

enter image description here

4
  • 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
  • 2
    @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
    +1 for nice answer! – Zarko Apr 17 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.