How to make a A4 sheet made of A7 pages?

I'm trying to make a booklet in A7 size but my printer only work with A4 so I need to make a pdf that look more or less like this:

Sheet 1 (Front)

+-----+-----+-----+-----+

|--40--|--1--|--38--|--3--|

+-----+-----+-----+-----+

|--36--|--5--|--34--|--7--|

+-----+-----+-----+-----+

Sheet 1 (Back)

+-----+-----+-----+-----+

|--39--|--2--|--37--|--4--|

+-----+-----+-----+-----+

|--35--|--6--|--33--|--8--|

+-----+-----+-----+-----+

Where the numbers represent the number of the page and the number of pages is 40. The question is what commands I have to use to make it a reality.

Sorry for my poor english

• Welcome! I'm afraid your project will take some time. First, do make sure that your A7 pages are not bleached or chemically treated as they will not be properly broken down by the bacteria etc. otherwise. I don't know how well it will work with pure paper and no rotting vegetables, so you might want to add some peelings or whatever you have handy. I'm not sure you can turn compost directly into paper, but you could use it to grow a tree. You could then use the tree to produce your A4 sheet. As I say, I hope you're not in a hurry! – cfr Aug 5 '16 at 2:03
• Nicely done, english isn't my first language I THINK that I corrected the mistake.Now serious can you help me? – Adriel Carlos Aug 5 '16 at 2:23
• Sorry. I have a twisted sense of humour. For the record, your English seems fine. (If I'd guessed English was not your first language, I wouldn't have left my earlier comment.) This is a duplicate I think. Let me see if I can find it. – cfr Aug 5 '16 at 2:46
• No problem I prefer that people show my mistakes so I can improve, about being a duplicate the close I got is this: tex.stackexchange.com/questions/261431/… but what I'm trying is a little diferent. – Adriel Carlos Aug 5 '16 at 2:58
• What the guy do in this tutorial: mostlymaths.net/2010/11/… is what I'm trying to do but I don't have a pdf but the text. Any help would be nice. – Adriel Carlos Aug 5 '16 at 3:06

This does not order the pages automatically, but it does seem to work.

Here's an A7 document with 40 pages. Call the result a7-40.pdf.

% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode
% arara: pdflatex
\pdfminorversion=7
\documentclass[twoside]{article}
\usepackage{geometry}
\geometry{paperwidth=74mm,paperheight=105mm}
\usepackage{graphicx}
\pagestyle{empty}
\begin{document}
\centering
\scalebox{30}{1}\newpage
\scalebox{30}{2}\newpage
\scalebox{30}{3}\newpage
\scalebox{30}{4}\newpage
\scalebox{30}{5}\newpage
\scalebox{30}{6}\newpage
\scalebox{30}{7}\newpage
\scalebox{30}{8}\newpage
\scalebox{30}{9}\newpage
\scalebox{10}{10}\newpage
\scalebox{10}{11}\newpage
\scalebox{10}{12}\newpage
\scalebox{10}{13}\newpage
\scalebox{10}{14}\newpage
\scalebox{10}{15}\newpage
\scalebox{10}{16}\newpage
\scalebox{10}{17}\newpage
\scalebox{10}{18}\newpage
\scalebox{10}{19}\newpage
\scalebox{10}{20}\newpage
\scalebox{10}{21}\newpage
\scalebox{10}{22}\newpage
\scalebox{10}{23}\newpage
\scalebox{10}{24}\newpage
\scalebox{10}{25}\newpage
\scalebox{10}{26}\newpage
\scalebox{10}{27}\newpage
\scalebox{10}{28}\newpage
\scalebox{10}{29}\newpage
\scalebox{10}{30}\newpage
\scalebox{10}{31}\newpage
\scalebox{10}{32}\newpage
\scalebox{10}{33}\newpage
\scalebox{10}{34}\newpage
\scalebox{10}{35}\newpage
\scalebox{10}{36}\newpage
\scalebox{10}{37}\newpage
\scalebox{10}{38}\newpage
\scalebox{10}{39}\newpage
\scalebox{10}{40}
\end{document}


Now use pdfpages to create the A4 pages:

\documentclass[a4paper,twoside]{article}
\usepackage{pdfpages,geometry}
\geometry{scale=1,marginparsep=0pt,marginparwidth=0pt,landscape}
\begin{document}
\includepdf[nup=4x2,pages={40,1,38,3,36,5,34,7,  32,9,30,11,28,13,26,15,  24,17,22,19,20,21,18,23,  16,25,14,27,12,29,10,31, 8,33,6,35,4,37,2,39}]{a7-40}
\end{document}


EDIT EDIT

Note that EDIT follows EDIT EDIT, which is logical but non-chronological.

To answer a query in comments, here is one way to add horizontal and vertical rules to divide the page evenly into quarters.

\documentclass[a4paper,twoside]{article}
\usepackage{pdfpages,geometry}
\geometry{scale=1,marginparsep=0pt,marginparwidth=0pt,landscape}
\begin{document}
\sbox\adrielhrule{\vrule height .2pt depth .2pt width \paperwidth}
\sbox\adrielvrule{\hspace*{-.2pt}\vrule height \paperheight depth 0pt width .4pt}
\includepdf[nup=4x2,pages={40,1,38,3,36,5,34,7,  32,9,30,11,28,13,26,15,  24,17,22,19,20,21,18,23,  16,25,14,27,12,29,10,31, 8,33,6,35,4,37,2,39}, picturecommand={%
\setlength\unitlength{1mm}% modified from Ignasi's answer at http://tex.stackexchange.com/a/64745/
}]{a7-40}
\end{document}


EDIT

To make this more convenient, it would be nice to have TeX figure out the page order and automatically add blank pages, if necessary, to produce a complete signature.

This solution uses expl3 and a little help from egreg to achieve just this.

\adrielbooklet[<other pdfpages options>]{<no. of pages>}{<pdf filename>}


will include the first <no. of pages> from <pdf filename>, arranging them in the appropriate order and applying <other pdfpages options>.

Complete code with examples:

\documentclass[a4paper,twoside]{article}
\usepackage{pdfpages,geometry,xparse}
\geometry{scale=1,marginparsep=0pt,marginparwidth=0pt,landscape}
\ExplSyntaxOn
{
\int_compare:nTF
{
\int_mod:nn { #1 } { 4 }  = 0
}
{
}
{
\int_set:Nn \l_adriel_total_int { #1 + 4 - \int_mod:nn { #1 } { 4 } }
}
\int_compare:nTF
{
}
{
}
{
}
\int_do_while:nn
{
}
{
{
\int_compare:nTF
{
}
{
}
{
}
}
{
\int_compare:nTF
{
}
{
}
{
}
}
}
}
{
\includepdf[#1]{#2}
}
{
\tl_put_right:Nn \l_adriel_pdfopts_tl { , #1 }
}
\NewDocumentCommand \adrielbooklet { O {} m m }
{
\adriel_booklet:nnn { #1 } { #2 } { #3 }
}
\ExplSyntaxOff
\begin{document}
\end{document}


This produces 4 booklets:

EDIT EDIT EDIT

Here's a (mostly) expl3 version with dividing lines:

\documentclass[a4paper,twoside]{article}
\usepackage{pdfpages,geometry,xparse}
\geometry{scale=1,marginparsep=0pt,marginparwidth=0pt,landscape}
\ExplSyntaxOn
{
\int_compare:nTF
{
\int_mod:nn { #1 } { 4 }  = 0
}
{
}
{
\int_set:Nn \l_adriel_total_int { #1 + 4 - \int_mod:nn { #1 } { 4 } }
}
\int_compare:nTF
{
}
{
}
{
}
\int_do_while:nn
{
}
{
{
\int_compare:nTF
{
}
{
}
{
}
}
{
\int_compare:nTF
{
}
{
}
{
}
}
}
}
{
\includepdf[#1]{#2}
}
{
\tl_put_right:Nn \l_adriel_pdfopts_tl { , #1 }
}
\NewDocumentCommand \adrielbooklet { O {} m m }
{
\adriel_booklet:nnn { #1 } { #2 } { #3 }
}
\hbox_set:Nn \l_adriel_hline_box { \vrule height .2pt depth .2pt width \paperwidth }
\hbox_set:Nn \l_adriel_vline_box { \hspace*{-.2pt} \vrule height \paperheight depth 0pt width .4pt }
{
}
{
}
\ExplSyntaxOff
\begin{document}
[nup=4x2,
picturecommand={%
\setlength\unitlength{1mm}% modified from Ignasi's answer at http://tex.stackexchange.com/a/64745/
}%
]{40}{a7-40}
\end{document}


• Great answer! In case one wants a semiautomatic way of generating the page interval, here's a Python one liner (just use the correct total of pages and run it in the terminal) python -c 'print((lambda h: reduce(lambda x, y : x + [ h + 1 - y, y ], range(1,h,2), []))(40))' – Paulo Cereda Aug 5 '16 at 14:44
• @PauloCereda That's really helpful, thanks. I was wondering about a way to do this within TeX (I don't know Python), but that is neat. – cfr Aug 5 '16 at 16:01
• Thanks, one more thing can you add some command so that the A4 show lines to horizontal and vertical a4 halfs? – Adriel Carlos Aug 5 '16 at 21:55
• @AdrielCarlos Please see either EDIT EDIT or EDIT EDIT EDIT above. EDIT may or may not be of interest and is sandwiched between EDIT EDIT and EDIT EDIT EDIT, just to confuse things. – cfr Aug 6 '16 at 21:51
• @PauloCereda That is also a lot more concise than the inline method I came up with above! – cfr Aug 6 '16 at 21:52

not sure if this fits the page rules, but you can probably do that with a tool like "pdf toolkit" (pdftk):

https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

in my opinion it is better to keep the output of tex linear and apply the reordering later for print, but i'm a noob. :-)

edit: actually pdfnup might be better suited:

http://manpages.ubuntu.com/manpages/wily/man1/pdfnup.1.html

• pdfpages uses pdftk (or it might be the other way around), but I definitely agree with the sentiment of keeping TeX output linear. The PDF should be readable – the printer should handle printing :) – Sean Allred Aug 6 '16 at 22:27