# How can I set page dimensions for a CWEB document?

I have a CWEB file and I want to change the page dimensions. I know how to do this in a plain TeX document, and it always worked fine for me. If I do the same (changing hsize, the offsets, etc.) in the CWEB file, I get completely screwed up margins and placement.

I want to have output in A4 paper. The final page dimensions are set by my pdftex to A4, so that is not the problem. The problem is rather that CWEB is made for letter paper output, so the copy is not centered, for instance.

I tried to look for a solution in Knuth's Stanford Graphbase, since it is a bunch of CWEB programs published in book format, so he must have done something to change page dimensions. The sources he makes available on the internet, however, are made for letter output, so they are of no help. Any ideas?

So far, I added the following code to the beginning of the CWEB file (this code works in a plain TeX file by the way):

\newdimen\pagewidth   \pagewidth=210mm
\newdimen\pageheight  \pageheight=297mm

\hsize=13cm

\hoffset=\pagewidth
\hoffset=.5\hoffset

\voffset=\pageheight
\voffset=0.5\voffset

• Show us the code you have got so far. – dexteritas Jun 29 '17 at 15:26

This is documented in the CWEB manual (which you can open by invoking texdoc cweb which opens a DVI file, or read as PDF online), in Appendix C: How to use CWEB macros (page 25), point 4.

The trick is that after setting any of \pagewidth (default 6.5in), \pageheight (default 8.7in) or \fullpageheight (default 9in), you need to say \setpage:

If you change any of these quantities, you should call the macro \setpage immediately after making the change.

Example: If you download lp.w from Knuth's downloadable programs page, and run cweave lp.w, you get an lp.tex, which processed with tex lp.tex (or pdftex lp.tex) gives the following as the first page:

If you change the top of lp.tex from

\input cwebmac
\datethis


to

\input cwebmac
\pagewidth=3.25in % The default is 6.5in
\pageheight=4.35in % The default is 8.7in
\fullpageheight=4.5in % The default is 9in
\setpage
\datethis


(halving everything), then (if you don't change the pdf paper size) you get the following:

For A4 paper specifically, if you want to keep the same margin as with letter paper (which may not make much sense, but is one option), then you can change the \input cwebmac at the top of the .tex file to (if you're using pdftex):

\input cwebmac
\pdfpagewidth=210mm
\pdfpageheight=297mm
\pagewidth= 159.2mm % The default is 6.5in for 8.5in width, so for A4 this is 210mm - 2in
\pageheight=238.58mm % The default is 8.7in for 11in height, so for A4 this is 297mm - 2.3in
\fullpageheight=246.2mm % The default is 9in for 11in height, so for A4 this is 297mm - 2in
\setpage


If you want to keep the layout (the text width, the line breaks, etc.) the same as for letter paper but instead squeeze and center that onto A4 paper (as suggested in Igor's answer), then of course what you need to do is nothing specific to cwebmac and you can use the same block of code everywhere: you can simply adjust \pdfhorigin: the default is 1in (=(8.5in - 6.5in)/2). Just add this at the top of your file and don't change anything else:

\pdfpagewidth=210mm
\pdfpageheight=297mm
\pdfhorigin=\dimexpr (210mm-6.5in)/2
\pdfvorigin=\dimexpr (297mm-9in)/2


Do not change page dimensions - change offsets to center the output on A4 page.

TeX does not care about "paper size". It is the job of the user to ensure that the output does not fall off the edge of the physical medium on which it is being printed through adjusting \hoffset, \hsize, \voffset and \vsize. I choose to adjust \hoffset and \voffset to preserve layout of documents using plain.tex if I want to print them on A4 paper.

Quoting TeXbook, page 251:

If you want your output to be positioned differently when it is ultimately printed, you can offset it by giving nonzero values to \hoffset and \voffset.

The idea is to set \hoffset and \voffset in format file so that default \hsize and \vsize will be centered on A4 page. But you also have to fix a bug in cwebmac.tex (it overwrites user settings for \hoffset).

To see this, perform the following steps:

cd <cweb_source>
cweave cweave.w
tex -ini -jobname tex '\input plain \hoffset=-1in \dump'
tex cweave.tex
dvi-viewer cweave.dvi


You will see that the margins are "jumping" from one page to another.

In the example I used \hoffset=-1in just for demonstration.

In reality, in my format file I have \hoffset=-2.95mm (\voffset is also changed in my format file, but in this case it does not matter).

The following code in the output routine overwrites user settings for \hoffset which are set in format file.

\ifodd\pageno\hoffset=\pageshift\fi


This code is supposed to be used for duplex printing, where even and odd pages should have different offset - to leave more space on the left side where binding is done.

When duplex printing is not used (i.e, when \hoffset and \pageshift are not set by the user in web file), there should be no offset.

In other words, by default (i.e., not in duplex mode), \hoffset must not change, i.e., it must have one and the same value throughout the whole TeX run.

This is guaranteed to be when \pageshift is equal to \hoffset by default, i.e., the code above becomes:

\ifodd\pageno\hoffset=\hoffset\fi


But with default settings, in such case the code is equivalent to

\ifodd\pageno\hoffset=0in\fi


which overwrites user settings.

The \hoffset etc... values are used during format gereration (tex -ini ...), which is separate from when TeX is run with the generated format and \input cwebmac is done.

So, to fix this problem, in cwebmac.tex instead of

\pageshift=0in


we should say

\pageshift=\hoffset


Besides, \hoffset is 0in by default, so there will be no problems.

You may also see cwebmal.tex and cwebmar.tex at github.com/igor-liferenko/cweb-git/

For completeness, following is the example of format file (omitting encTeX settings and setting catcodes for non-ascii letters)

\hoffset=-2.95mm % (210mm - 6.5in)/2 - 1in = (210mm - 8.5in)/2
\voffset=8.8mm % (297mm - 11in)/2
\catcode\@=11
\def\m@g{\mag\count@
\hsize6.5truein
\vsize8.9truein
\dimen\footins8truein
\hoffset=-2.95truemm
\voffset=8.8truemm}
\catcode\@=12
\dump


UPDATE the bug was fixed in cweb-3.64c

• Interesting answer, but I disagree with how you consider \hoffset and \voffset: they are not meant for dealing with different page sizes, but for adjusting the output in case printer drivers have different ideas about it. Also, \pdfoutput is always defined in pdftex, so the test will succeed even if DVI output is in force. – egreg Jun 30 '17 at 12:46
• I think you're right that the current definition in cwebmac.tex is non-ideal, as it basically assumes \hoffset is zero. Changing the default of \pageshift is probably not the right solution though, as it changes the documented meaning. (“The \pageshift macro defines an amount by which right-hand pages (i.e., odd-numbered pages) are shifted right with respect to left-hand (even-numbered) ones.”) So we want something like \newdimen\oddoffset \oddoffset=\hoffset \advance\oddoffset by \pageshift and changing the if to \ifodd\pageno\hoffset=\oddoffset\fi (not tested). What do you think? – ShreevatsaR Jun 30 '17 at 17:36
• I don't get it: I made a file with the code above and included it in my cweb source with \input, but I still get a left margin that is bigger than the right margin. What is going on? – Fernando Jun 30 '17 at 17:56
• @Fernando see UPDATE – Igor Liferenko Aug 5 '17 at 9:44