{"id":177,"date":"2011-01-09T14:40:47","date_gmt":"2011-01-09T14:40:47","guid":{"rendered":"http:\/\/www.readytext.co.uk\/?p=177"},"modified":"2013-11-28T08:58:01","modified_gmt":"2013-11-28T08:58:01","slug":"latex-page-layout-parameters-part-2","status":"publish","type":"post","link":"https:\/\/www.readytext.co.uk\/?p=177","title":{"rendered":"LaTeX page layout parameters (Part 2)"},"content":{"rendered":"<p>In <a href=\"https:\/\/www.readytext.co.uk\/?p=93\">Part 1<\/a> of this tutorial we looked at the relationship between LaTeX&#8217;s page layout parameters and the conventional &#8220;DTP&#8221; layout model used by designers to parameterise page layout (refer to <a href=\"https:\/\/www.readytext.co.uk\/files\/layout.svg\">this diagram<\/a> for a refresher). So, the next step is how do we use the formulae in <a href=\"https:\/\/www.readytext.co.uk\/?p=93\">Part 1<\/a> to do something useful? In this tutorial I will show one way to do this using a Lua script and LuaTeX.<\/p>\n<blockquote><p><strong>LuaTeX: a primer<\/strong><br \/>\nIn future tutorials I will explain how to get started with a minimal LuaTeX setup but for present purposes I will have to assume that you have a working LuaTeX installation, perhaps via <a href=\"http:\/\/www.tug.org\/texlive\/\">TeX Live<\/a>, <a href=\"http:\/\/miktex.org\/\">MiKTeX<\/a> or something similar. In essence, LuaTeX is derived from, and extends, <a href=\"http:\/\/www.tug.org\/applications\/pdftex\/\">pdfTeX<\/a> through the integration of Lua as an embedded scripting language and a <a href=\"http:\/\/www.luatex.org\/roadmap.html\"> whole host of additional functionality<\/a>. It is the integration of the Lua scripting language which, in my opinion, makes LuaTeX so powerful and interesting.  To run Lua code you use a LuaTeX command called <code>\\directlua{...}<\/code> where <code>...<\/code> is your Lua code. Through the <code>\\directlua{...}<\/code> command you can execute your own Lua code and access the &#8220;LuaTeX API&#8221; which provides a huge range of libraries and functions that give you access to, and control of, many aspects of TeX&#8217;s internals through Lua code. This is an extremely simplistic overview, so should you want more information, grab a copy of the <a href=\"http:\/\/www.luatex.org\/svn\/trunk\/manual\/luatexref-t.pdf\">LuaTeX Reference Manual<\/a> which is constantly updated as the LuaTeX program evolves. Note that the LuaTeX Reference Manual is a highly technical document which presents the APIs and libraries, it is <em>not <\/em>a user guide and is written by programmers for programmers.<\/p><\/blockquote>\n<h4>Where are we heading?<\/h4>\n<p>The goal is to create a simple setup so that you can define custom pages and margins through LaTeX code such as this:<\/p>\n<p><code>\\documentclass[11pt,twoside]{article}<br \/>\n\\input setpage<br \/>\n\\setpage{300}{485}{250}{255}{5}{5}{5}{5}<br \/>\n\\begin{document}<br \/>\n....<br \/>\n\\end{document}<\/code><\/p>\n<p>Where <code>\\input setpage <\/code> inputs a file (called <code>setpage.tex<\/code>) which contains the macro to define a command called <code>\\setpage<\/code>. This takes a number of arguments to define your custom page and margins. Of course, this should be wrapped up into a proper LaTeX package but that is left as an exercise for the reader. <\/p>\n<p>So, what do we need to do?<\/p>\n<ol>\n<li>Create a Lua script that will perform calculations of the formulae presented in Part 1.<\/li>\n<li>Use <code>\\directlua{...}<\/code> to run the Lua script in (1) via LuaTeX.<\/li>\n<li>Combine (1) and (2) into a very simple command (<code>\\setpage<\/code>) you can use in your LaTeX document to define custom pages.<\/li>\n<\/ol>\n<h4>Create a Lua script<\/h4>\n<p>The starting point is, of course, that we want to have any size of paper with a document (book, business card etc) centred on the paper area. Because LuaTeX is based on pdfTeX it uses the parameters <code>\\pdfpagewidth<\/code> and <code>\\pdfpageheight<\/code> to set the width and height of the paper (i.e., the PDF page size). See <a href=\"http:\/\/sarovar.org\/docman\/view.php\/106\/64\/pdftex-a.pdf.pdf\">the pdfTeX user manual<\/a> for details. So, using <code>\\pdfpagewidth<\/code> and <code>\\pdfpageheight<\/code> we can set the size of the paper we want to use. Next, we need to know the page size of the document we want to produce. With the PDF paper size and document page size we can calculate the values of \u0394X and \u0394Y to centre the document on our paper. The following SVG graphic (displayed via an iframe) shows the scenario we are aiming for.<\/p>\n<p><iframe src =\"..\/files\/paperandpage.svg\" width=\"100%\" height=\"750\"><\/p>\n<p>Your browser does not support iframes.<\/p>\n<p><\/iframe><\/p>\n<p>So, our starting point is a set of page layout variables from the &#8220;DTP design world&#8221; which we will use to calculate the appropriate LaTeX page parameters. Our &#8220;DTP design world&#8221; parameters are:<\/p>\n<ul>\n<li>PaperWidth = the value of <code>\\pdfpagewidth<\/code><\/li>\n<li>PaperHeight = this is the value of <code>\\pdfpageheight<\/code><\/li>\n<li>BookPageWidth = your document&#8217;s page width<\/li>\n<li>BookPageHeight = your document&#8217;s page height<\/li>\n<li>BookOuterMargin = B<small style=\"position: relative;  bottom: -.3em;\">OM<\/small> (refer to <a href=\"https:\/\/www.readytext.co.uk\/files\/layout.svg\">this diagram<\/a> for a refresher)<\/li>\n<li>BookInnerMargin = B<small style=\"position: relative;  bottom: -.3em;\">IM<\/small> (refer to <a href=\"https:\/\/www.readytext.co.uk\/files\/layout.svg\">this diagram<\/a> for a refresher)<\/li>\n<li>BookTopMargin = B<small style=\"position: relative;  bottom: -.3em;\">TM<\/small> (refer to <a href=\"https:\/\/www.readytext.co.uk\/files\/layout.svg\">this diagram<\/a> for a refresher)<\/li>\n<li>BookBottomMargin = B<small style=\"position: relative;  bottom: -.3em;\">BM<\/small> (refer to <a href=\"https:\/\/www.readytext.co.uk\/files\/layout.svg\">this diagram<\/a> for a refresher)<\/li>\n<\/ul>\n<p>Using the &#8220;DTP design world&#8221; values, we need to calculate the following LaTeX page layout parameters:<code>\\textwidth, \\topmargin, \\oddsidemargin, \\evensidemargin<\/code> and <code>\\textheight<\/code>.<\/p>\n<p>The following code shows one simple Lua script that will do the job. It defines a Lua function <code>calcvals(arg)<\/code> which has a single argument called <code>arg <\/code>. In Lua-speak <code>arg <\/code> is a table so that when we call the function <code>calcvals({...})<\/code>, the data in braces <code>{...}<\/code> is passed in as the value of <code>arg<\/code> and will contain a number of values which are accessed using the standard Lua method for accessing table values, such as <code>arg.PaperWidth<\/code> and  <code>arg.BookPageWidth<\/code>. The code also sets some LaTeX parameters to 0 and defines &#8220;OneInch&#8221; as 25.4 &ndash; note that we are working with units in mm, just because it is convenient; hence 1 inch is 25.4 mm.<\/p>\n<p>The most important point to note is the line <\/p>\n<p><code>local marg = assert(io.open(\"path_to_your_tex_setup\/margins.tex\",\"w\"))<\/code><\/p>\n<p>We are creating a file called <code>margins.tex<\/code> which will define all the LaTeX parameters to achieve our preferred page layout. It will subsequently be input into our LaTeX document via TeX code such as <code>\\input margins.tex<\/code>, hence you will need to output <code>margins.tex<\/code> into a location where the LuaTeX engine can find it. <\/p>\n<p><iframe name='iframe1' id=\"iframe1\" src =\"..\/files\/code.lua\" width=\"100%\" height=\"300\"><\/p>\n<p>Your browser does not support iframes.<\/p>\n<p><\/iframe><\/p>\n<h4>Use <code>\\directlua{...}<\/code> to run the Lua script<\/h4>\n<p>OK, at this point we have a Lua script which takes, as input, our &#8220;DTP design world&#8221; values and uses them to calculate, and write out, the LaTeX page layout values into a file called <code>margins.tex<\/code>. The next question is how do we get LuaTeX to run this Lua script? Answer, of course, <code>\\directlua{...}<\/code>! Let us assume that the Lua code above is saved into a file called, say, <code>code.lua<\/code>.<\/p>\n<blockquote><p><strong>loadfile(): a primer<\/strong><br \/>\nAt this point I need to briefly explain a Lua function called <a href=\"http:\/\/www.lua.org\/pil\/8.html\"><code> loadfile()<\/code><\/a>. Quoting from the official documentation &#8220;&#8230; loadfile also loads a Lua chunk from a file, but it does not run the chunk. Instead, it only compiles the chunk and returns the compiled chunk as a function.&#8221; What this means is that calling <code>loadfile(code.lua)<\/code> does not actually execute the code in <code>code.lua<\/code> but compiles it and returns a function that you need to run in order to actually execute <code>code.lua<\/code> and so gain access to the <code>calcvals(arg)<\/code> function we defined in our Lua script. This may all sound a bit weird if you have not programmed in Lua but like most descriptions, it sounds more difficult that it actually is in practice. <\/p><\/blockquote>\n<p>Ignoring the LuaTeX side of things for the moment, concentrating just on Lua, to gain access to the function <code>calcvals(arg)<\/code> defined and stored in <code>code.lua<\/code> we have to<\/p>\n<ol>\n<li>run <code>loadfile(\"path_to_your_tex_setup\/code.lua\")<\/code><\/li>\n<li>strictly speaking, make sure the <code>code.lua<\/code> loaded without error (check error return from <code>loadfile(...)<\/code>)<\/li>\n<li>if successful, run the function returned from the call to <code>loadfile(\"path_to_your_tex_setup\/code.lua\")<\/code><\/li>\n<\/ol>\n<p><code>pagecalcs, loaderror = loadfile(\"path_to_your_tex_setup\/code.lua\")<br \/>\n-- you should check loaderror.<br \/>\n-- if pagecalcs == nil there was an error.<br \/>\n-- the error text will be in loaderror.<br \/>\n-- if pagecalcs is not nil, then code.lua compiled OK.<br \/>\n-- and will be in pagecalcs as function we can call as pagecalcs().<br \/>\n<\/code><\/p>\n<p>Assuming <code>loadfile(\"path_to_your_tex_setup\/code.lua\")<\/code> is successful, we can execute the returned function <code>pagecalcs()<\/code> which will enable us to use the <code>calcvals(arg)<\/code> function we defined and stored in <code>code.lua<\/code>.  <\/p>\n<h5>Back to <code>\\directlua{...}<\/code><\/h5>\n<p>After the <code>loadfile(...)<\/code> detour we can now look at wrapping this up into something we can use with LuaTeX via <code>\\directlua{...}<\/code><\/p>\n<p>As discussed, <code>\\directlua{...}<\/code> is the command provided by LuaTeX which is, in effect, our &#8220;interface&#8221; or &#8220;gateway&#8221; to running Lua code from within a TeX document. The minimal LuaTeX code to run <code>code.lua<\/code> would be<\/p>\n<p><code><br \/>\n\\directlua {%<br \/>\n          pagecalcs, loaderror= loadfile(\"path_to_your_tex_setup\/code.lua\")<br \/>\n          pagecalcs()<br \/>\n}<br \/>\n<\/code><\/p>\n<h4>And finally&#8230; <code>\\setpage<\/code> <\/h4>\n<p>With no further ado, here is the LuaTeX code for <code>setpage.tex<\/code>. You&#8217;ll notice that <code>\\setpage<\/code> takes 8 TeX parameters:<\/p>\n<p><code>\\def\\setpage#1#2#3#4#5#6#7#8<\/code><\/p>\n<p>which will be explained below.<\/p>\n<p><iframe name='iframe2' id=\"iframe2\" src =\"..\/files\/setpage._tex\" width=\"100%\" height=\"300\"><\/p>\n<p>Your browser does not support iframes.<\/p>\n<p><\/iframe><\/p>\n<p>The 8 parameters for <code>\\setpage#1#2#3#4#5#6#7#8<\/code> are passed straight into <code>\\directlua{...}<\/code> so that our Lua function <code>calcvals(arg)<\/code> can use those 8 TeX parameters to do all the calculations, which result in the <code>margins.tex<\/code> file containing the LaTeX page parameters. This is where TeX meets Lua! The key thing to observe is the call to <code>calcvals({...})<\/code>. We mentioned earlier that when we defined <code>calcvals(arg)<\/code> in our Lua file <code>code.lua<\/code> that <code>arg <\/code>was a Lua table, now we can see this in action. The <code><strong>code in bold<\/strong><\/code> is the actual value of <code>arg<\/code>:<\/p>\n<p><code><br \/>\n                         calcvals(<strong>{<br \/>\n                         PaperWidth=#1,<br \/>\n                         PaperHeight=#2,<br \/>\n                         BookPageWidth=#3,<br \/>\n                         BookPageHeight=#4,<br \/>\n                         BookOuterMargin=#5,<br \/>\n                         BookInnerMargin=#6,<br \/>\n                         BookTopMargin=#7,<br \/>\n                         BookBottomMargin=#8<br \/>\n}<\/strong>)<br \/>\n<\/code><\/p>\n<p>In addition, the definition of <code>calcvals(arg)<\/code> in our Lua file <code>code.lua<\/code> made <code>calcvals(arg)<\/code> return a number of values. Note that a nice feature of the Lua language is that it will return multiple values from function calls. <\/p>\n<p><code>deltax, deltay, textwidth,topmargin,oddsidemargin,evensidemargin,textheight =<br \/>\ncalcvals({PaperWidth=#1, PaperHeight=#2, BookPageWidth=#3, BookPageHeight=#4, BookOuterMargin=#5,<br \/>\nBookInnerMargin=#6, BookTopMargin=#7, BookBottomMargin=#8})<\/code><\/p>\n<p>Why might we want to return those values? Well, if you look at <code>code.lua<\/code> carefully, you will see that it creates another table called <code>pagevars <\/code>and stores various page layout parameters in that table. The reason for doing this is to make the <code>pagevars <\/code> table available to other Lua scripts in our LuaTeX document. What we create is a Lua table which saves useful page parameter values we can use for other things, such as printers marks because we now have easy access to the necessary data.    <\/p>\n<p><code>pagevars={}<br \/>\npagevars[\"paperwidth\"]=#1<br \/>\npagevars[\"paperheight\"]=#2<br \/>\npagevars[\"bookpagewidth\"]=#3<br \/>\npagevars[\"bookpageheight\"]=#4<br \/>\npagevars[\"bookoutermargin\"]=#5<br \/>\npagevars[\"bookinnermargin\"]=#6<br \/>\npagevars[\"booktopmargin\"]=#7<br \/>\npagevars[\"bookbottommargin\"]=#8<br \/>\npagevars[\"deltay\"]=deltay<br \/>\npagevars[\"deltax\"]=deltax<br \/>\npagevars[\"textwidth\"]=textwidth<br \/>\npagevars[\"topmargin\"]=topmargin<br \/>\npagevars[\"oddsidemargin\"]=oddsidemargin<br \/>\npagevars[\"evensidemargin\"]=evensidemargin<br \/>\npagevars[\"textheight\"]=textheight}<\/code><\/p>\n<h3>Conclusion and example<\/h3>\n<p>To use all of this you will need to make sure that the TeX and Lua code is placed in locations where your LuaTeX executable can find them. In a future tutorial I will explain how to set up a minimal LuaTeX system on Windows (sorry, I don&#8217;t work on Linux or Mac). Here is a mimimal file demonstrating the use of <code>\\setpage<\/code><\/p>\n<p><code>\\documentclass[11pt,twoside]{article}<br \/>\n\\input setpage<br \/>\n\\setpage{300}{485}{250}{255}{5}{5}{5}{5}<br \/>\n\\begin{document}<br \/>\nHello Lua\\TeX\\<br \/>\n\\end{document}<\/code><\/p>\n<p>Looking back at our code for <code>calcvals(arg)<\/code><\/p>\n<p><code><br \/>\n                         calcvals(<strong>{<br \/>\n                         PaperWidth=#1,<br \/>\n                         PaperHeight=#2,<br \/>\n                         BookPageWidth=#3,<br \/>\n                         BookPageHeight=#4,<br \/>\n                         BookOuterMargin=#5,<br \/>\n                         BookInnerMargin=#6,<br \/>\n                         BookTopMargin=#7,<br \/>\n                         BookBottomMargin=#8<br \/>\n}<\/strong>)<br \/>\n<\/code><\/p>\n<p>we can see that <code>\\setpage<\/code> is called with the following values (note: all assumed to be in mm!)<\/p>\n<p><code>\\setpage{PaperWidth}{PaperHeight}{BookPageWidth}{BookPageHeight}{BookOuterMargin}{BookInnerMargin}{BookTopMargin}{BookBottomMargin}<\/code><\/p>\n<p>So, this is where we hoped to get to: using &#8220;DTP world&#8221; parameters to achieve the equivalent layout in LaTeX. <\/p>\n<h4>Example<\/h4>\n<p><code><br \/>\n\\documentclass[11pt,twoside]{article}<br \/>\n\\input setpage<br \/>\n\\setpage{500}{500}{300}{300}{10}{10}{10}{10}<br \/>\n\\begin{document}<br \/>\n\\pagestyle{empty}<br \/>\nHello Lua\\TeX<br \/>\n\\end{document}<\/code><\/p>\n<p>Here is the <a href = \"https:\/\/www.readytext.co.uk\/files\/demoa.pdf\">PDF document<\/a> output by LuaTeX but note that the printers marks code is not included in this example, that&#8217;s for another day!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Part 1 of this tutorial we looked at the relationship between LaTeX&#8217;s page layout parameters and the conventional &#8220;DTP&#8221; layout model used by designers to parameterise page layout (refer to this diagram for a refresher). So, the next step is how do we use the formulae in Part 1 to do something useful? In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,11],"tags":[],"class_list":["post-177","post","type-post","status-publish","format-standard","hentry","category-luatex","category-page-design"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=177"}],"version-history":[{"count":104,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/177\/revisions"}],"predecessor-version":[{"id":3231,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/177\/revisions\/3231"}],"wp:attachment":[{"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.readytext.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}