TransWikia.com

Make inline questions that are later printed again as flashcards

TeX - LaTeX Asked by taffel on February 10, 2021

I am working on a handout in the tufte-book document class but wanted to make flashcards inline that are both printed within the text in one format (front and back side-by-side to show what you are expected to learn) and printed as proper flashcards in the appendix. I found the flacards document class that seemed perfect when combined with scontents.

The problem is that I cannot use two document classes in one single document, and I cannot compile the two documents independently because the content is defined with scontents. Can anyone help me find a better approach?

So, I have my main.tex

documentclass[a4paper]{article}

usepackage[store-env=flashcards,store-cmd=flashcards]{scontents}
setupsc{print-env=false}
usepackage{forloop}

newcommand{flash}[2]{%
Scontents{#1}Scontents{#2}%
Side 1: #1

Side 2: #2
}

begin{document}

flash{A}{B}
flash{C}{D}

clearpage
include{flashcards}

end{document}

and then flashcards.tex

documentclass[frontgrid]{flacards}

begin{document}

newcounter{ct}
newcounter{ct2}
newcounter{maxct}
setcounter{maxct}{countsc{flashcards}}
forloop[2]{ct}{1}{value{ct} < value{maxct}}%
{%
  setcounter{ct2}{value{ct}}
  addtocounter{ct2}{1}
  card{getstored[value{ct}]{flashcards}}{getstored[value{ct2}]{flashcards}}
}

end{document}

One Answer

I was able to solve it by converting the flacards class into a package, and tweak the geometries a bit. So this solution only works for A4 paper. The result is that I can add flash{A}{B} to my document and get

Flashcards as they appear inline

inline and

Flashcards as they appear in the appendix

in the appendix.

In the preamble, add

documentclass[notoc,nols,a4paper]{tufte-book}
...
usepackage[store-env=flashcards,store-cmd=flashcards]{scontents}
setupsc{print-env=false}
usepackage{forloop}

newcommand{flash}[2]{%
Scontents{#1}Scontents{#2}%
noindent%
begin{minipage}{80mm}
  begin{flashfront}
    #1
  end{flashfront}
end{minipage}% This must go next to `end{minipage}`
begin{minipage}{80mm}
  begin{flashback}
    #2
  end{flashback}
end{minipage}
}

usepackage{tikz}
usepackage[framemethod=TikZ]{mdframed}

newenvironment{flashfront}[1][]{%
ifstrempty{#1}%
{mdfsetup{%
frametitle={%
tikz[baseline=(current bounding box.east),outer sep=0pt]
node[anchor=east,rectangle,fill=blue!20]
{strut Front};}}
}%
{mdfsetup{%
frametitle={%
tikz[baseline=(current bounding box.east),outer sep=0pt]
node[anchor=east,rectangle,fill=blue!20]
{strut Front:~#1};}}%
}%
mdfsetup{innertopmargin=10pt,linecolor=blue!20,%
linewidth=2pt,topline=true,%
frametitleaboveskip=dimexpr-htstrutboxrelax
}
begin{mdframed}[]relax%
sloppy
}{end{mdframed}}

newenvironment{flashback}[1][]{%
ifstrempty{#1}%
{mdfsetup{%
frametitle={%
tikz[baseline=(current bounding box.east),outer sep=0pt]
node[anchor=east,rectangle,fill=red!20]
{strut Back};}}
}%
{mdfsetup{%
frametitle={%
tikz[baseline=(current bounding box.east),outer sep=0pt]
node[anchor=east,rectangle,fill=red!20]
{strut Back:~#1};}}%
}%
mdfsetup{innertopmargin=10pt,linecolor=red!20,%
linewidth=2pt,topline=true,%
frametitleaboveskip=dimexpr-htstrutboxrelax
}
begin{mdframed}[]relax%
sloppy
}{end{mdframed}}

Then, where we want the flashcards to appear

chapter*{Flashcards}
newcounter{ct}
newcounter{ct2}
newcounter{maxct}
setcounter{maxct}{countsc{flashcards}}
forloop[2]{ct}{1}{value{ct} < value{maxct}}%
{%
  setcounter{ct2}{value{ct}}
  addtocounter{ct2}{1}
  card{getstored[value{ct}]{flashcards}}{getstored[value{ct2}]{flashcards}}
}
newgeometry{textwidth=20cm,noheadfoot, margin=1cm, outer=1cm, inner=1cm}
cleardoublepage
pagestyle{empty}
boxfrontnewpageboxback
restoregeometry

Create a file called myflash.sty


deffc@frontgrid{true}
deffc@backgrid{true}
deftextwidthh{190mm}

%TODO: margin managment
%RequirePackage[noheadfoot, margin=1cm, outer=1cm, inner=1cm]{geometry}

% TODO: replace
newcommand*{fc@strippt}[1]{%
    strip@pt#1}%

newcounter{cardno}setcounter{cardno}{0}
newcountcardperpage
newdimencardwidth
newdimencardheight

newcommand{fc@boxcontent}[2]{%
parindent=0pt%
{cardbox{#1}{#2}}%
}

newcommand{cardbox}[2]{%
vbox{%
vskipfboxsep%
hbox{%
hskipfboxsep%
{advancecardheight by -2fboxsep
vbox to cardheight {%
hsize=cardwidthadvancehsize by -2fboxsep%
{cardinnerbox{#1}{#2}}}%
}hskipfboxsep}}%
}

newcommand{cardinnerbox}[2]{%
 sloppy%
 %smash%
 {%
 makebox[0pt][l]{parbox[t]{cardwidth}{raggedrightcsname #1lheadendcsname}}hfill%
 makebox[0pt][c]{parbox[t]{cardwidth}{centeringcsname #1cheadendcsname}}hfill%
 makebox[0pt][r]{parbox[t]{cardwidth}{raggedleftcsname #1rheadendcsname}}%
 }%
 vfill%
 {cardtext{#1}{#2}}
 vfill%
 %smash%
 {%
 makebox[0pt][l]{parbox[b]{cardwidth}{raggedrightcsname #1lfootendcsname}}hfill%
 makebox[0pt][c]{parbox[b]{cardwidth}{centeringcsname #1cfootendcsname}}hfill%
 makebox[0pt][r]{parbox[b]{cardwidth}{raggedleftcsname #1rfootendcsname}}%
 }%
}

newcommand{cardtext}[2]{%
centering{csname cardtextstyle#1endcsname #2}%
}

newcommand{cardtextstylef}{%
itshape}

newcommand{cardtextstyleb}{%
itshape}

%% front
newsaveboxfront
setboxfront=vbox{}
% layout
newcommand{flhead}{}newcommand{fchead}{}newcommand{frhead}{}
newcommand{flfoot}{}newcommand{fcfoot}{}newcommand{frfoot}{}

%% back
newsaveboxback
setboxback=vbox{}
% layout 
newcommand{blhead}{}newcommand{bchead}{}newcommand{brhead}{}
newcommand{blfoot}{}newcommand{bcfoot}{}newcommand{brfoot}{}

renewcommand{frfoot}{footnotesize thecardno}
renewcommand{brfoot}{footnotesize thecardno}

newcommand{pagesetup}[2]{%
defrowsperpage{#2}relax%
defcolsperpage{#1}relax%
cardwidth=textwidthhrelax%
cardheight=textheightrelax%
dividecardwidth by colsperpage%
dividecardheight by rowsperpage%
cardperpage=colsperpagerelax%
multiplycardperpage by rowsperpagerelax%
}

newcommand{card}[2]{%
@tempcnta=-thecardnodivide@tempcnta by cardperpagemultiply@tempcnta by cardperpageadvance@tempcnta by thecardnorelax%
@tempcntb=@tempcnta%
count@=@tempcntarelax%
dividecount@ by colsperpagerelax%
@tempcnta=count@relax%
multiplycount@ by colsperpagerelax%
advance@tempcntb by -count@relax%
stepcounter{cardno}%
unitlength=1pt%
advance@tempcnta by 1relax%
@tempdima=cardheightrelax%
@tempdimb=cardwidthrelax%
advance@tempdima by fboxrulerelax%
advance@tempdimb by fboxrulerelax%
multiply@tempdima by @tempcntarelax%
multiply@tempdimb by @tempcntbrelax%
advance@tempdima by -baselineskiprelax%
setboxfront=vbox{unvboxfront%
noindentbegin{picture}(0,0)%
put(fc@strippt@tempdimb,-fc@strippt@tempdima){%
ifdefinedfc@frontgridframebox(fc@stripptcardwidth,fc@stripptcardheight)[t]{fc@boxcontent{f}{#1}}
elsemakebox(fc@stripptcardwidth,fc@stripptcardheight)[t]{fc@boxcontent{f}{#1}}%
fi
}%
end{picture}%
}%
setboxback=vbox{unvboxback%
noindentbegin{picture}(0,0)%
multiply@tempdimb by -1relax%
advance@tempdimb by textwidthhrelax%
advance@tempdimb by -cardwidthrelax%
put(fc@strippt@tempdimb,-fc@strippt@tempdima){%
ifdefinedfc@backgridframebox(fc@stripptcardwidth,fc@stripptcardheight)[t]{fc@boxcontent{b}{#2}}%
elsemakebox(fc@stripptcardwidth,fc@stripptcardheight)[t]{fc@boxcontent{b}{#2}}%
fi%
}%
end{picture}%
}%
count@=thecardnodividecount@ by cardperpagemultiplycount@ by cardperpageadvancecount@ by -thecardnorelax%
ifnum count@=0relax%
noindentboxfrontnewpageboxbacknewpage%
elsefi
}

Answered by taffel on February 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP