4

I have the following table which compiles perfectly

\documentclass{article}

\usepackage{booktabs} \usepackage[margin=32mm]{geometry} \usepackage{tabularx} \usepackage{float}

\newcolumntype{Y}{>{\centering\arraybackslash}X} \begin{document} \begin{table}[H]\centering \begin{tabularx}{\textwidth}{ccYYcYY@{}} \toprule && \multicolumn{2}{c}{Next State} && \multicolumn{2}{c}{Output} \ \cmidrule{3-4} \cmidrule{6-7} Present State && $x = 0$ & $x = 1$ && $x = 0$ & $x = 1$\ \midrule 000 && 010 & 011 && 0 & 0 \ 001 && 001 & 001 && 1 & 1 \ 010 && 110 & 011 && 0 & 0 \ 011 && 000 & 101 && 1 & 1 \[0.5em] 100 && 111 & 101 && 0 & 0 \ 101 && 000 & 101 && 0 & 1 \ 110 && 000 & 001 && 1 & 0 \ 111 && 101 & 011 && 0 & 0 \ \bottomrule \end{tabularx} \caption{State table for the sequential circuit discussed} \end{table} \end{document}

Based on this I want to make an environment so I can reuse this table (the number of columns don’t have to change). I have tried defining it like so (I have simply extracted the begin and end parts of the table and left out the contents):

\newenvironment{statetable}%
{\begin{table}[H]\centering
    \begin{tabularx}{\textwidth}{ccYYcYY@{}}
        \toprule
        && \multicolumn{2}{c}{Next State} && \multicolumn{2}{c}{Output} \\
        \cmidrule{3-4} \cmidrule{6-7}
        Present State && $x = 0$ & $x = 1$ && $x = 0$ & $x = 1$ \\
        \midrule}%
{\bottomrule
    \end{tabularx}
    \caption{State table for the sequential circuit discussed}%
\end{table}}

So I can use it like this:

% \begin{statetable}
 000   &&  010    &   011  &&   0    &    0    \\
 001   &&  001    &   001  &&   1    &    1    \\
 010   &&  110    &   011  &&   0    &    0    \\
 011   &&  000    &   101  &&   1    &    1    \\[0.5em]
 100   &&  111    &   101  &&   0    &    0    \\
 101   &&  000    &   101  &&   0    &    1    \\
 110   &&  000    &   001  &&   1    &    0    \\
 111   &&  101    &   011  &&   0    &    0    \\
% \end{statetable}

But I get the following error:

ABD: EveryShipout initializing macros
! Misplaced alignment tab character &.
l.52  000   &
             &  010    &   011  &&   0    &    0    \\
?
  • 1
    I’m on mobile so can’t try this myself, but as a first step for debugging this, I suggest making it more minimal. Try a similar thing, but with a much smaller table/custom environment — take out almost all the table content, cut down to 1 or 2 columns, etc. If it still fails then, post that as your MWE — it’ll be much easier for experts to spot the issue. Or if it works when minimised, put the content back in gradually, and see which bit specifically triggers the problem. – Peter LeFanu Lumsdaine Mar 27 '21 at 09:27
  • @FirstUser -- the answer below is a rough attempt to point you in the right direction -- reference -- https://tex.stackexchange.com/questions/489099/defining-a-custom-environment-with-tabularx – js bibra Mar 27 '21 at 09:29

2 Answers2

4

enter image description here

\documentclass{article}
\usepackage{tabularx}
\usepackage{float}
\usepackage{booktabs}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\newenvironment{statetable}% {\table \tabularx{\textwidth}{cYYYY} \toprule & \multicolumn{2}{c}{Next State} & \multicolumn{2}{c}{Output} \ \cmidrule(lr){2-3} \cmidrule(l){4-5} Present State & $x = 0$ & $x = 1$ & $x = 0$ & $x = 1$ \ \midrule }% {\bottomrule \endtabularx \caption{State table for the sequential circuit discussed}% \endtable}

\newenvironment{altstatetable}% {\table \begin{tabular}{\textwidth}{c@{\extracolsep{\fill}}cccc} \toprule & \multicolumn{2}{c}{Next State} & \multicolumn{2}{c}{Output} \ \cmidrule(r){2-3} \cmidrule{4-5} Present State & $x = 0$ & $x = 1$ & $x = 0$ & $x = 1$ \ \midrule }% {\bottomrule \end{tabular} \caption{State table for the sequential circuit discussed}% \endtable}

\begin{document} \begin{statetable} 000 & 010 & 011 & 0 & 0 \ 001 & 001 & 001 & 1 & 1 \ 010 & 110 & 011 & 0 & 0 \ 011 & 000 & 101 & 1 & 1 \[0.5em] 100 & 111 & 101 & 0 & 0 \ 101 & 000 & 101 & 0 & 1 \ 110 & 000 & 001 & 1 & 0 \ 111 & 101 & 011 & 0 & 0 \ \end{statetable}

\begin{altstatetable} 000 & 010 & 011 & 0 & 0 \ 001 & 001 & 001 & 1 & 1 \ 010 & 110 & 011 & 0 & 0 \ 011 & 000 & 101 & 1 & 1 \[0.5em] 100 & 111 & 101 & 0 & 0 \ 101 & 000 & 101 & 0 & 1 \ 110 & 000 & 001 & 1 & 0 \ 111 & 101 & 011 & 0 & 0 \ \end{altstatetable}

\end{document}


Using \begin{tabularx} and \end{tabularx} inside of a \newenvironment does not work. This is also explained in the tabularx documentation from which the following is quoted:

This mechanism of grabbing an environment body does have the disadvantage (shared with the AMS alignment environments) that you can not make extension environments by code such as

\newenvironment{foo}{\begin{tabularx}{XX}}{\end{tabularx}}

as the code is looking for a literal string \end{tabularx} to stop scanning. Since version 2.02, one may avoid this problem by using \tabularx and \endtabularx directly in the definition:

\newenvironment{foo}{\tabularx{XX}}{\endtabularx}

I have applied the same technique for the table environment, as well.

Additonally, I also removed the redundant empty columns and introduced a gap in the adjacent \cmidrule commands using the trim options ((l) and (r), for more information on that, see the booktabs documentation).

Since you dind't seem to need linebreaks inside of your columns, I decided to add an alternative version of your table using tabular* in combination with \extracolsep{\fill}} and regular c type columns instead of tabularx.

leandriis
  • 62,593
  • Thanks this is working perfectly. I didn't know that you could avoid having to write \begin{envname} and \end{envname} in the newenvironment definition blocks. I can do this for all environments right ? Also could you please tell me what I was doing wrong ? – First User Mar 27 '21 at 10:49
  • 1
    @FirstUser: I added some short explanations on what I changed and why. I hope this helps. – leandriis Mar 27 '21 at 11:11
2

enter image description here

\documentclass{article}
\usepackage{tabularx}
\usepackage{environ, booktabs}
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\NewEnviron{note}{ \begin{tabularx}{\textwidth}{ccYYcYY@{}} \toprule Present State && $x = 0$ & $x = 1$ && $x = 0$ & $x = 1$\ \midrule 000 && 010 & 011 && 0 & 0 \ \bottomrule \end{tabularx} }

\begin{document} \begin{note}

\end{note}

\end{document}

js bibra
  • 21,280