| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \margins {Macros for margins setting <2023-05-01>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ---------------------------- |
| 6 |
\`\margins``/<pg> <fmt> (<left>,<right>,<top>,<bot>)<unit>` |
| 7 |
takes its parameters, does calculation and sets `\hoffset`, `\voffset`, |
| 8 |
`\hsize` and `\vsize` registers. Note that \OpTeX/ sets the page origin at |
| 9 |
the top left corner of the paper, no at the obscure position 1\,in, 1\,in. |
| 10 |
It is much more comfortable for macro writers. |
| 11 |
\_cod ---------------------------- |
| 12 |
|
| 13 |
\_newdimen\_pgwidth \_newdimen\_pgheight \_pgwidth=0pt |
| 14 |
\_newdimen\_shiftoffset |
| 15 |
|
| 16 |
\_def\_margins/#1 #2 (#3,#4,#5,#6)#7 {\_def\_tmp{#7}% |
| 17 |
\_ifx\_tmp\_empty |
| 18 |
\_opwarning{\_string\_margins: missing unit, mm inserted}\_def\_tmp{mm}\_fi |
| 19 |
\_setpagedimens #2 % setting \_pgwidth, \_pgheight |
| 20 |
\_ifdim\_pgwidth=0pt \_else |
| 21 |
\_hoffset=0pt \_voffset=0pt |
| 22 |
\_if$#3$\_if$#4$\_hoffset =\_dimexpr (\_pgwidth -\_hsize)/2 \_relax |
| 23 |
\_else \_hoffset =\_dimexpr \_pgwidth -\_hsize - #4\_tmp \_relax % only right margin |
| 24 |
\_fi |
| 25 |
\_else \_if$#4$\_hoffset = #3\_tmp \_relax % only left margin |
| 26 |
\_else \_hsize =\_dimexpr \_pgwidth - #3\_tmp - #4\_tmp \_relax % left+right margin |
| 27 |
\_hoffset = #3\_tmp \_relax |
| 28 |
\_xhsize =\_hsize \_setxhsize % \_xhsize used by \output routine |
| 29 |
\_fi\_fi |
| 30 |
\_if$#5$\_if$#6$\_voffset =\_dimexpr (\_pgheight -\_vsize)/2 \_relax |
| 31 |
\_else \_voffset =\_dimexpr \_pgheight -\_vsize - #6\_tmp \_relax % only bottom margin |
| 32 |
\_fi |
| 33 |
\_else \_if$#6$\_voffset = #5\_tmp \_relax % only top margin |
| 34 |
\_else \_vsize=\_dimexpr \_pgheight - #5\_tmp - #6\_tmp \_relax % top+bottom margin |
| 35 |
\_voffset = #5\_tmp \_relax |
| 36 |
\_fi\_fi |
| 37 |
\_if 1#1\_shiftoffset=0pt \_def\_prepoffsets{}\_else \_if 2#1% double-page layout |
| 38 |
\_shiftoffset = \_dimexpr \_pgwidth -\_hsize -2\_hoffset \_relax |
| 39 |
\_def\_prepoffsets{\_ifodd\_pageno \_else \_advance\_hoffset \_shiftoffset \_fi |
| 40 |
\_setpagerightoffset}% |
| 41 |
\_else \_opwarning{use \_string\_margins/1 or \_string\_margins/2}% |
| 42 |
\_fi\_fi\_fi |
| 43 |
\_setpagerightoffset |
| 44 |
} |
| 45 |
\_def\_setpagedimens{\_isnextchar({\_setpagedimensB}{\_setpagedimensA}} |
| 46 |
\_def\_setpagedimensA#1 {\_ifcsname _pgs:#1\_endcsname |
| 47 |
\_ea\_ea\_ea\_setpagedimensB \_csname _pgs:#1\_ea\_endcsname\_space |
| 48 |
\_else \_opwarning{page specification "#1" is undefined}\_fi} |
| 49 |
\_def\_setpagedimensB (#1,#2)#3 {\_setpagedimensC\_pgwidth=#1:#3 |
| 50 |
\_setpagedimensC\_pgheight=#2:#3 |
| 51 |
\_pdfpagewidth=\_pgwidth \_pdfpageheight=\_pgheight |
| 52 |
} |
| 53 |
\_def\_setpagedimensC #1=#2:#3 {#1=#2\_ifx^#3^\_tmp\_else#3\_fi\_relax\_truedimen#1} |
| 54 |
|
| 55 |
\_public \margins ; |
| 56 |
|
| 57 |
\_doc ---------------------------- |
| 58 |
The common page dimensions are defined here. |
| 59 |
\_cod ---------------------------- |
| 60 |
|
| 61 |
\_sdef{_pgs:a3}{(297,420)mm} \_sdef{_pgs:a4}{(210,297)mm} \_sdef{_pgs:a5}{(148,210)mm} |
| 62 |
\_sdef{_pgs:a3l}{(420,297)mm} \_sdef{_pgs:a4l}{(297,210)mm} \_sdef{_pgs:a5l}{(210,148)mm} |
| 63 |
\_sdef{_pgs:b5}{(176,250)mm} \_sdef{_pgs:letter}{(8.5,11)in} |
| 64 |
|
| 65 |
\_doc ---------------------------- |
| 66 |
\`\magscale` `[<factor>]` does `\mag=<factor>` and recalculates page |
| 67 |
dimensions to their true values.\nl |
| 68 |
\`\_truedimen``<dimen-register>` returns true value of <dimen-register> |
| 69 |
regardless of `\mag`. |
| 70 |
\_cod ---------------------------- |
| 71 |
|
| 72 |
\_def\_trueunit{} |
| 73 |
\_def\_magscale[#1]{\_mag=#1\_def\_trueunit{true}% |
| 74 |
\_ifdim\_pgwidth=0pt \_else \_truedimen\_pgwidth \_truedimen\_pgheight \_fi |
| 75 |
\_truedimen\_pdfpagewidth \_truedimen\_pdfpageheight |
| 76 |
} |
| 77 |
\_def\_truedimen#1{\_ifx\_trueunit\_empty \_else#1=\_ea\_ignorept\_the#1truept \_fi} |
| 78 |
|
| 79 |
\_public \magscale ; |
| 80 |
|
| 81 |
\_doc ----------------------------- |
| 82 |
When left-to-right direction of typesetting is selected (default) then |
| 83 |
\"main vertical line" of the page has `\hoffset` distance from the left |
| 84 |
paper border and all lines at the page start here and run to the right side |
| 85 |
(exceptions can be done by `\moveleft` or `\moveright`, of course). When we |
| 86 |
have set right-to-left direction (using `\textdir TRT`, for example), then |
| 87 |
the \"main vertical line" cannot be at the same position because lines run to the left, |
| 88 |
i.e.\ they would be off paper. This is reason why the setting `\pagedir TRT` shifts the |
| 89 |
\"main vertical line" to an alternative position: it has `\pagerightoffset+1in` distance from the |
| 90 |
{\em right} paper border and thus right-to-left lines are visible on the |
| 91 |
paper. We have to set `\pagerightoffset` properly for such cases. This is |
| 92 |
done in the macro \`\_setpagerightoffset`. |
| 93 |
It must be called whenever `\hoffset` is changed. |
| 94 |
\_cod ----------------------------- |
| 95 |
|
| 96 |
\_def\_setpagerightoffset{% |
| 97 |
\_pagerightoffset=\_dimexpr\_pdfpagewidth-\_xhsize-\_hoffset-1in\_relax |
| 98 |
} |
| 99 |
\_setpagerightoffset % setting default value from default values |
| 100 |
|
| 101 |
\_doc ----------------------------- |
| 102 |
Page numbers and numbers of (sub)sections have to be printed in |
| 103 |
left-to-right mode even though the document mode is right-to-left. |
| 104 |
We print these numbers via \`\_numprint``{<number>}` in \OpTeX/ macros. |
| 105 |
The `\_numprint` is `\_useit` by default (i.e.\ do nothing special) |
| 106 |
because we have left-to-right mode as default. But a user can define |
| 107 |
\begtt |
| 108 |
\_def\_numprint#1{{\_textdir TLT #1}} |
| 109 |
\endtt |
| 110 |
if the document is set to right-to-left mode. |
| 111 |
\_cod ----------------------------- |
| 112 |
|
| 113 |
\_let\_numprint=\_useit |
| 114 |
|
| 115 |
\_endcode % ----------------------------------------- |
| 116 |
|
| 117 |
2023-05-01 \_numprint for page numbers, section numbers introduced |
| 118 |
2023-04-29 \_setpagerightoffset for TRT typesetting introduced again |
| 119 |
2021-03-15 \_setxhsize added |
| 120 |
2020-03-14 released |