# How to extract each pair of two adjacent cells of a table into a separate tight page?

For the sake of simplicity, I provide a table as follows. I cannot make it simpler because simplification probably no longer match my real scenario.

The code below was written by me and modified by David Carlisle in another thread here. The link will be provided later if needed.

\documentclass{article}

\usepackage{longtable,array,xcolor}
\renewcommand*\arraystretch{10}
\newcounter{counter}

\newcolumntype\specifier{|%
*2{
>{\centering\selectfont\stepcounter{counter}\thecounter}m{1cm}
|>{\centering}m{\dimexpr.5\linewidth-1cm-4\tabcolsep-3\arrayrulewidth\relax}
|}
}

\makeatletter
\def\row[#1]#2{%
& #1 \par \vspace{15pt} \textcolor{red}{#2}
\ifodd\value{counter}
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{&}{\tabularnewline\hline}
}

\let\oldendlongtable\endlongtable
\def\endlongtable{%
\ifodd\value{counter}\omit\tabularnewline\cline{1-2}\fi
\oldendlongtable}

\begin{document}
\begin{longtable}{\specifier}\hline
\row[RAM]{Random Access Memory}
\row[BIOS]{Basic Input Output System}
\row[OMG]{Oh My Ghost}
\row[PS]{Post Script}
% and many more
\end{longtable}
\end{document}


As the real scenario is to create a flash card, I have to extract each two adjacent cells into a separate, tight page (with some border for aesthetic purpose).

Example,

• cell (1,1) and cell (1,2) are extracted into a single, tight page.
• cell (1,3) and cell (1,4) are extracted into another single, tight page.
• cell (2,1) and cell (2,2) are extracted into another single, tight page.
• cell (2,3) and cell (2,4) are extracted into another single, tight page.
• etc.

How to do so? Or you can suggest me to use whatever you think it becomes better.

• How flexible does the solution need to be? Are you just looking for something that will suit you in this case, or does it have to be for arbitrary values for e.g. height, width, amount, etc.? Oct 18, 2014 at 8:49
• @1010011010: For the sake of simplicity, just make the size remain unchanged. Adding a border of 12pt around each output page should be fine enough. Oct 18, 2014 at 9:11

\documentclass{article}

\usepackage{xcolor}

\newcounter{counter}

\makeatletter
\def\row[#1]#2{%
\setbox0
\hbox{\kern12pt\vbox{\kern12pt\hrule height\arrayrulewidth
\hbox{\vline\kern\tabcolsep
\parbox[c][10\baselineskip]{1cm}{\centering\stepcounter{counter}\thecounter}%
\kern\tabcolsep\vline\kern\tabcolsep
\parbox{\dimexpr.5\linewidth-1cm-4\tabcolsep-3\arrayrulewidth\relax}%
{\centering #1\par\vspace{15pt}\textcolor{red}{#2}}%
\kern\tabcolsep\vline}%
\hrule height\arrayrulewidth
\kern12pt}%
\kern12pt}%
\pdfpageheight\ht0
\pdfpagewidth\wd0
{\hoffset-1in\voffset-1in\shipout\box0 }}

\def\longtable#1#2{}
\def\enflongtable{}

\begin{document}
\begin{longtable}{\specifier}\hline
\row[RAM]{Random Access Memory}
\row[BIOS]{Basic Input Output System}
\row[OMG]{Oh My Ghost}
\row[PS]{Post Script}
% and many more
\end{longtable}
\end{document}


This version includes a toggle following \documentclass. If this is set to true, then individual cards are produced using TiKZ. If it is set to false, then the old layout is used. The code requires the --shell-escape option or equivalent as it writes the contents of the cards to an external file and then reads them back in using textmerg.

I have never written anything to an external file before myself so I am not at all sure that this is a proper way to do it. I based the code on a question with an answer by egreg but I may well have introduced my own varieties of mangling.

Caveat emptor...

\documentclass{article}
% The next line defines and sets the toggle. Remove the % to get the original layout; replace it to get individual cards.
\newif\ifcards\cardstrue%\cardsfalse

\usepackage{array,xcolor,calc}

\renewcommand*\arraystretch{10}
\newcounter{counter}

\newlength\sidewidth
\newlength\cardwidth
\newlength\cardheight
\setlength\sidewidth{10mm + 2\tabcolsep + 2\arrayrulewidth}

\newcolumntype\specifier{|%
*2{
>{\centering\selectfont\stepcounter{counter}\thecounter}m{1cm}
|>{\centering}m{\dimexpr.5\linewidth-1cm-4\tabcolsep-3\arrayrulewidth\relax}
|}
}

\makeatletter

\newif\if@mywriteopen % initially false http://tex.stackexchange.com/questions/42204/help-wanted-on-openout-write-and-closeout

\def\row[#1]#2{%
& #1 \par \vspace{15pt} \textcolor{red}{#2}
\ifodd\value{counter}
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{&}{\tabularnewline\hline}}

\ifcards
\usepackage{geometry,tikz,textmerg}
\usetikzlibrary{calc}

\newwrite\mywriter

\def\@writecard#1{% http://tex.stackexchange.com/questions/42204/help-wanted-on-openout-write-and-closeout
\if@mywriteopen\else
\global\@mywriteopentrue
\immediate\openout\mywriter=mycards.dat\relax
\fi
\immediate\write\mywriter{#1}}

\settototalheight{\cardheight}{%
\begin{tabular}{\specifier}\hline
\let\@writecard\relax
\let\stepcounter\relax
\row[RAM]{Random Access Memory}
\end{tabular}}
\settowidth{\cardwidth}{%
\begin{tabular}{\specifier}\hline
\let\@writecard\relax
\let\stepcounter\relax
\row[RAM]{Random Access Memory}
\end{tabular}}
\def\row[#1]#2{%
\ifcards
\stepcounter{counter}%
\@writecard{\thecounter}%
\@writecard{#1}%
\@writecard{#2}%
\fi}

\geometry{%
paperwidth=1.25\cardwidth,
paperheight=1.25\cardheight,
textheight=.4pt+\cardheight,
textwidth=.4pt+\cardwidth,
hratio=1:1,
vratio=1:1,
}
\setlength\parindent{0pt}

\newenvironment{flashcards}{%
\setcounter{counter}{0}%
}{%
\immediate\closeout\mywriter\relax
\Fields{\flashnumber\flashacro\flashcontent}
\Merge{mycards.dat}{%
\pgfkeyssetvalue{/flashcards/number}{\flashnumber}
\pgfkeyssetvalue{/flashcards/acro}{\flashacro}
\pgfkeyssetvalue{/flashcards/content}{\flashcontent}
\begin{tikzpicture}[outer sep=0pt, inner sep=0pt]
\path [draw]
(0,0) coordinate (a) -| (\cardwidth,-\cardheight) coordinate [midway] (c) coordinate (d) -| cycle
(a) +(\sidewidth,0) coordinate (b) -- (b |- d)
;
\node [minimum width=\sidewidth, minimum height=.75\cardheight, anchor=south west, text centered] at (a |- d) {\pgfkeysvalueof{/flashcards/number}};
\node [minimum width={\cardwidth-\sidewidth}, minimum height=.75\cardheight, anchor=south west, text centered, align=center] at (b |- d) {\pgfkeysvalueof{/flashcards/acro}\\[15pt]\color{red}\pgfkeysvalueof{/flashcards/content}};
\end{tikzpicture}}}

\else
\usepackage{longtable}

\let\oldendlongtable\endlongtable
\def\endlongtable{%
\ifodd\value{counter}\omit\tabularnewline\cline{1-2}\fi
\oldendlongtable}

\newenvironment{flashcards}{%
\begin{longtable}{\specifier}\hline
}{%
\end{longtable}
}
\fi

\makeatother

\begin{document}
\begin{flashcards}%
\row[RAM]{Random Access Memory}%
\row[BIOS]{Basic Input Output System}%
\row[OMG]{Oh My Ghost}%
\row[PS]{Post Script}%
% and many more
\end{flashcards}%
\end{document}


Setting \cardsfalse produces:

Here is a tikz version:

## Code:

\documentclass{article}

\usepackage{longtable,xcolor}
\newcounter{counter}
\usepackage{tikz}

%% Select the page geometry
\newcommand{\Margin}{5pt}
\usepackage[paperheight=5cm,paperwidth=7cm,margin=\Margin,nofoot]{geometry}

\newcommand{\row}[2][]{%
\stepcounter{counter}%
\noindent%
\begin{tikzpicture}[font=\bfseries]
\draw [gray, thick] (1.0cm,\Margin) -- (1.0,\paperheight-\Margin-2\pgflinewidth);
\draw [rounded corners=10pt, ultra thick] (0,\Margin) rectangle (\linewidth-\Margin,\paperheight-\Margin-\pgflinewidth);
\node [align=center] at (0.5cm,0.5\paperheight-\Margin)  {\thecounter};
\node [align=center] at (0.5cm+0.5\linewidth-\Margin,0.5\paperheight-\Margin)  {#1 \\[15pt] \textcolor{red}{#2}};
\end{tikzpicture}%
\newpage%
}

\renewenvironment{longtable}[1]{\def\hline{}}{}%
\pagestyle{empty}

\begin{document}
\begin{longtable}{\specifier}\hline
\row[RAM]{Random Access Memory}
\row[BIOS]{Basic Input Output System}
\row[OMG]{Oh My Ghost}
\row[PS]{Post Script}
% and many more
\end{longtable}
\end{document}

• A nice candidate to be accepted. :-) Oct 30, 2014 at 3:41
• Nice. Why do you need to change the definition of \hline? Why not just not issue the command?
– cfr
Oct 30, 2014 at 3:45
• @cfr: But why issue the \hline? Oct 30, 2014 at 4:30
• Isn't that what I asked you?
– cfr
Oct 30, 2014 at 4:32
• @cfr: :-) I don't think a horizontal line is needed, plus if I don't suppress it, I end up with  Misplaced \noalign.. Oct 30, 2014 at 4:34

Is this what you're looking for? It's an INCREDIBLY static solution but it seems to do what you ask for.....

Also, the code is practical rather than elegant.

\documentclass{article}

\usepackage{longtable,array,xcolor}
\renewcommand*\arraystretch{10}
\newcounter{counter}
\pagenumbering{gobble}% the number is already shown on the flashcard, no need for pagenumbering here

\newcolumntype\specifier{|%
*2{
>{\centering\stepcounter{counter}\thecounter}m{1cm}
|>{\centering}m{147.3pt}% this is now a static value, since linewidth changes along with pagesize
|}
}

\usepackage{tcolorbox}

\makeatletter
\def\row[#1]#2{%
& #1 \par \vspace{15pt} \textcolor{red}{#2}
\tabularnewline\hline%changed this, your ifeven and ifodd construction wants to progress sideways then downwards, which is not useful for your request
}

\let\oldendlongtable\endlongtable
\def\endlongtable{%
\ifodd\value{counter}\omit\tabularnewline\cline{1-2}\fi
\oldendlongtable}

\newdimen\border \border=12pt

\usepackage[
paperheight=\dimexpr156pt+2\border,
paperwidth=\dimexpr201.4pt+2\border,
margin=\border,
top=\dimexpr-9.8pt+\border,
left=\dimexpr-1pt+\border
]{geometry}%tightness of the page, static for now

\begin{document}

\begin{longtable}{\specifier}\hline
\row[RAM]{Random Access Memory}
\row[BIOS]{Basic Input Output System}
\row[OMG]{Oh My Ghost}
\row[PS]{Post Script}
\end{longtable}

\end{document}

• You changed the size. Oct 18, 2014 at 9:50
• @InPSTrickswetrust You use \linewidth in your code which is dynamic. Calculating your previous linewidth and other dimensions I get to 147.3pt. Oct 18, 2014 at 9:55
• @InPSTrickswetrust I changed some things around to keep your spacing and size intact and I introduced a newdimen which allows you to control the border. I don't know. Hopefully it suits your needs. Otherwise I'd definitely like to see what others can come up with. :-) Oct 18, 2014 at 10:27