Debian Codegraph @

texmf-dist/source/lualatex/placeat/placeat.dtx

raw
1 % \iffalse
2 %<*driver>
3 %% File: placeat.dtx by Arno Trautmann, mail: arno dot trautmann at gmx dot de
4 \iffalse
5 %</driver>
6 %<*readme>
7 This is the README file of the placeat package. The package provides several commands to place content at an arbitrary position of the current page in any document. For orientation, a grid is drawn. Simple graphical elements like arrows and circles can easily be drawn and positioned.
8
9 To produce the package files, run lualatex on placeat.dtx, wich should result in the creation of the following files:
10 placeat.pdf (documentation)
11 placeat.sty (LaTeX user interface)
12 placeat.lua (Lua package code)
13
14 You need an up-to-date TeX Live (2017 at best) and compilation with LuaLaTeX to use this package. A full, updated MiKTeX should also work. (Not tested.)
15
16 For any comments or suggestions, contact me:
17 arno dot trautmann at gmx dot de
18
19 Hope you have fun with this package!
20
21 This package is copyright © 2017 Arno L. Trautmann. It may be distributed and/or
22 modified under the conditions of the LaTeX Project Public License, either version 1.3c
23 of this license or (at your option) any later version. This work has the LPPL mainten-
24 ance status ‘maintained’.
25 %</readme>
26 %<*driver>
27 \fi
28 \def\nameofplainTeX{plain}
29 \ifx\fmtname\nameofplainTeX\else
30 \expandafter\begingroup
31 \fi
32 \input docstrip.tex
33 \keepsilent
34 \let\MetaPrefix\relax
35 \preamble
36
37 EXPERIMENTAL CODE
38
39 This package is copyright © 2017 Arno L. Trautmann. It may be distributed and/or
40 modified under the conditions of the LaTeX Project Public License, either version 1.3c
41 of this license or (at your option) any later version. This work has the LPPL mainten-
42 ance status ‘maintained’.
43 \endpreamble
44 \let\MetaPrefix\DoubleperCent
45 \postamble
46 \endpostamble
47 \askforoverwritefalse
48
49 \generate{\file{placeat.sty}{\from{placeat.dtx}{package}}}
50 \generate{\file{placeat.tex}{\from{placeat.dtx}{tex}}}
51 \def\MetaPrefix{-- }
52 \def\luapostamble{%
53 \MetaPrefix^^J%
54 \MetaPrefix\space End of File `\outFileName'.%
55 }
56 \def\currentpostamble{\luapostamble}%
57
58 \generate{\file{placeat.lua}{\from{placeat.dtx}{lua}}}
59 \nopreamble\nopostamble
60 \catcode`\@12
61
62 \generate{\file{README.}{\from{placeat.dtx}{readme}}}
63 % readme will not be generated until I figured out how to write a file without extension
64
65 \ifx\fmtname\nameofplainTeX
66 \expandafter\endbatchfile
67 \else
68 \expandafter\endgroup
69 \fi
70 \documentclass[11pt,english]{ltxdoc}
71 \usepackage{
72 array,
73 babel,
74 booktabs,
75 fontspec, %% has to be in this order because of strange check in fixltl2e …
76 float,
77 geometry,
78 graphicx,
79 hyperref,
80 microtype,
81 scrpage2,
82 tabu
83 }
84 \usepackage[svgnames]{xcolor}
85 \hypersetup{
86 colorlinks = true,
87 linkcolor = blue,
88 filecolor = blue,
89 urlcolor = blue
90 }
91
92 \cfoot{\pagemark}
93 \ohead{}
94 \pagestyle{scrheadings}
95 \setmainfont{Linux Libertine O}
96 \setsansfont{Linux Biolinum O}
97
98 \usepackage{placeat}
99 \placeatsetup{nogrid}
100
101 \def\ALT{%
102 \bgroup%
103 \fontspec{Latin Modern Sans}%
104 A%
105 \kern-.37em \raisebox{.7ex}{\scalebox{0.25}{L}}%
106 \kern-.0em \raisebox{-0.98ex}{T}%
107 \egroup%
108 }
109
110 \begin{document}
111 \centerline{\fontsize{40}{55}\selectfont place\ALT}
112
113 \centerline{v0.1d1}
114 \centerline{\hspace*{2cm} Arno L. Trautmann}
115 \centerline{\href{mailto:[email protected]}{[email protected]}}
116
117 \kern.5cm
118 \begin{abstract}
119 The package \textsf{placeat} offers the command \verb|\placeat(2,5){}| which places arbitrary content freely on any page. It is mainly thought for use with the beamer class but may also be used with any other \LaTeX\ class. This package requires Lua\LaTeX; don't try it with any other \TeX\ flavour, it just won't work.
120 \end{abstract}
121
122 \begin{figure}[b]
123 \small\noindent
124 \fbox{\parbox{.97\textwidth}{
125 This package is copyright © 2017 Arno L. Trautmann. It may be distributed and/or
126 modified under the conditions of the LaTeX Project Public License, either version 1.3c
127 of this license or (at your option) any later version. This work has the LPPL maintenance status ‘maintained’. Whoever notes the face in the title gets a cookie when we meet.}}
128 \end{figure}
129
130 \textcolor{red}{\textls{ATTENTION}} This package is in a very preliminary version and released for testing only. Any feature might be subject to changes without notice.
131
132 \section*{Introduction}
133 This is the documentation of the package |placeat|. When you load the package, a grid is drawn on every page of your document to aid you at placing stuff where you want it to be. This mainly makes sense in presentations, but might be used in any document. The main macro of this package \verb|\placeat...{}| offers several ways to use it:
134 \begin{verbatim}
135 \placeat<D4>{some content}
136 \placeat(3,4){some content}
137 \placeat{3}{4}{some content}
138 \end{verbatim}
139
140 To deactivate the grid, use the setup command \verb|\placeatsetup{nogrid}|. There are also some other commands that allow you to draw simple sketches which might be useful in presentations, too, like arrows, circles etc., but no fancy stuff.
141
142 \vspace{1cm}
143
144 \textcolor{red}{\emph{Attention}}: This package is under development and everything presented here might and will be subject to incompatible changes.
145
146 If you have any suggestions or comments, just drop me a mail, I’ll be happy to get any response! The latest source code is hosted on github – Feel free to comment or report bugs there, to fork, pull, etc.:
147 \url{https://github.com/alt/placeat}\\
148
149 \newpage
150 \tableofcontents
151 \newpage
152 \part{User Documentation}
153 \section{How do I use it?}
154 \subsection{Placing – the Main Commands}
155 The command \verb|\placeat| takes several arguments, the last of which is the content you want to place:
156 \begin{verbatim}
157 \placeat(4,5){content}
158 \end{verbatim}
159 This may range from single letters to graphic objects or (mostly) any valid \LaTeX\ code. Take note that the content will be placed \emph{above} and \emph{right of}\footnote{See below for placing to the left via an optional argument.} the specified coordinates.\footnote{To be more precise, the ground line of the first line of the content is placed at the specified vertical coordinate. This may result in strange placement of anything that is not pure text.} Exceptions are floating environments – you have to pack them into a minipage or similar construct, see below.
160
161 If you want to place something \emph{left of} the specified coordinates, there is an additional optional argument to \verb|\placeat|:
162
163 \begin{verbatim}
164 \placeat{4}{5}[left]{right}
165 \end{verbatim}
166
167 This allows you to center your content (by hand) around the given place. Do not forget to enter an empty \verb|{}| if you use only the optional content.
168
169 Verbatim material does definitely \emph{not} work and makes troubles as always in moving arguments (like footnotes etc.). So far I have no idea how to handle that correctly. Please tell me any further problems, I'll happily tackle them or sadly note them here if I cannot fix it …
170
171 You may use \verb|\placeat| in one of the following variants (feel free to mix them in one document):
172 \begin{verbatim}
173 \placeat<D5>{content-right}
174 \placeat(4,5){content-right}
175 \placeat{4}{5}{content}
176 \end{verbatim}
177 The result will be the same in all three cases, so it's just a matter of taste which one you choose. They all will place the \verb|<content>| at a position that is specified by the grid which is drawn on your document. While the grid is drawn using letters and numbers, you might prefer using two numbers as you then also can use decimals for fine tuning which is not possible with a letter-number combination:
178 \begin{verbatim}
179 \placeat{4.3}{5.2}{content}
180 \placeat(4.3,5.2){content}
181 \end{verbatim}
182 Finally, there is one more argument you can give as second-to-last argument:
183 \begin{verbatim}
184 \placeat{4.3}{5.2}[content-left]{content}
185 \placeat(4.3,5.2)[content-left]{content}
186 \end{verbatim}
187 This content will be placed to the left of the specified coordinates as opposed to the normal content expanding to the right.
188
189 \subsection{Relative Placing}
190 It is also possible to place a second element relative to another one. For this, you have to give the first one a name and refer to this name in the second one. Then you can repeat and refer a third one to the second one (or the first one, however you like to).
191 \begin{verbatim}
192 \placeat(4,5){content}[first]
193 \placerelto[first](2,2){content2}[second]
194 \placerelto[second]{2}{2}{content3}[third]
195 \end{verbatim}
196 Although it does not make any sense, you still can use the chess-pattern notation for |\placerelto|. But that's just for raising the obscurity level of this package.
197
198 \subsection{Placing of figures, floats etc.}
199 Placing figures might be a bit tricky because the placing actually places the \emph{groundline} of any object. You may make your life easier when inserting figures if you use the |[t]| argument:
200 \begin{verbatim}
201 \placeat{4}{5}{\includegraphics[t]{bose-gas}}
202 \end{verbatim}
203 This way it is easier to fit graphics at the same height. However, you might have to test where it lands in the end.
204
205 For floating environments, even if they don't float (that would be stupid, wouldn't it?), you need to packg them into e.\,g. a minipage. You can do this by hand or just use the command \verb|\placeminipageat|. This command only has one kind of interface, the one with two braces:
206 \begin{verbatim}
207 \placeminipageat{4}{5}[4cm]{content}
208 \end{verbatim}
209 Here, the third, argument is optional and specifies the width of the minipage. If not given, it will default to 10cm, which should be wide enough to contain anything you ever want to set using \verb|placeat|.
210
211 \subsection{User Options}
212 Some of this package's features can be adjusted at any time in the document with the command
213 \begin{verbatim}
214 \placeatsetup{}
215 \end{verbatim}
216 Some of the options only make sense when used in the preamble, others only have a result when used in the text. However, none should result in an error, so feel free to do whatever nonesens you want to.
217
218 \subsection{The Grid}
219 If the number of grid lines does not suit you (there are ten horizontally and vertically), you can increase or decrease the number by
220 \begin{verbatim}
221 \placeatsetup{gridnumber = 12}
222 \end{verbatim}
223 You may change the gridnumber during your document, but don't expect everything to work fine.
224
225 The horizontal and vertical gridnumbers can be adjusted independently:
226 \begin{verbatim}
227 \placeatsetup{
228 gridnumberx = 12,
229 gridnumbery = 8,
230 }
231 \end{verbatim}
232
233 The grid can be deactivated by the document options \verb|final| or \verb|nogrid| and re-activated by the option \verb|drawgrid| in the setup macro:
234 \begin{verbatim}
235 \placeatsetup{nogrid}
236 \placeatsetup{drawgrid}
237 \end{verbatim}
238
239 \subsection{Offsetting}
240 You can choose the zero point of the grid by setting the options
241 \begin{verbatim}
242 \placeatsetup{
243 offsetx = 2
244 offsety = -1
245 }
246 \end{verbatim}
247 The grid and placement are adapted correspondingly. If you are a C-head thinking that everything should start with 0 instead of 1, you can call
248 \begin{verbatim}
249 \placeatsetup{
250 startzero
251 }
252 \end{verbatim}
253 which corresponds to |offsetx = 1,offsety = 1| so that the upper right corner has coordinates |(0,0)| instead of |(1,1)|.
254
255
256 \section{Drawing simple forms}
257 This package also allows to draw simple forms like arrows and circles, to support the user e.\,g. when creating presentations. A single line is drawn by calling
258 \begin{verbatim}
259 \placelineat(2.5,1.5)(1.5,2.5)
260 \end{verbatim}
261 where the first coordinate pair specifies the start of the line and the second one the end. As you typically need fine tuning to place the line exactly where you want it, it is not possible to use another interface, i.\,e. the \verb|<D4>| style.
262
263 By now, the following commands and respective forms are possible:
264 \begin{table}[H]
265 \begin{tabu}{>{\ttfamily\textbackslash}lX}
266 \toprule
267 placelineat(x1,y1)(x2,y2) & Draws a single line pointing from |(x1,y1)| to |(x2,y2)|\\\midrule
268 placearrowat(x1,y1)(x2,y2) & As the line, but with an arrowhead at the end.\\\midrule
269 placecircleat(x,y)\{r\} & Draws a circle at position |(x,y)| with diameter |r|. If omitted, |r| will default to |3|. The diameter is not scaled to the same scale as the coordinates, and most likely you have to test what size fits. Start with~5, it's a nice number. Right now, the circle is not really a circle, but slightly deformed as we only have cubic splines. May change to something better.\\\midrule
270 placesquareat(x,y)\{r\} & Draws a square with center at |(x,y)| and side lingth |r|. If omitted, |r| will default to |3|.\\\midrule
271 placerectangleat(x1,y1)(x2,y2) & draws a rectangle from the (upper left) corner |(x1,y1)| to the (lower right) corner |(x2,y2)|.\\\midrule
272 placefilledrectangleat(x1,y1)(x2,y2) & draws a filled rectangle.
273 \\\bottomrule
274 \end{tabu}
275 \end{table}
276
277 You can change the linewidth and therefore the thickness of lines with the simple call
278 \begin{verbatim}
279 \placeatsetup{linewidth=5}
280 \end{verbatim}
281 Default is 1, I have no idea in which unit, but it is a very nice thickness, I think. You can change the thickness any time and as often as you want.
282
283 Missing are eliptical shapes, maybe rounded corners for the rectangles and maybe some funny stuff.\footnote{Yes, I \emph{will} add a penis-shape macro, but that will not be documented explicitely.} The arrowheads need a lot of work, too, of course.
284
285 \subsection{Colored forms}
286 You need to load the \textsc{xcolor} package to use colors.\footnote{Why is it not required in the \textsc{placeat} package? Because you might want to specify package options and that may collide with the loading here. However, every sane document working with color requires the package by default.} Every command of the ones listed above takes an optional argument that allows the specification of a color. This is based on the \textsc{xcolor}, so all colors known by that package are possible:
287
288 \begin{verbatim}
289 \placecircleat[blue](5,5)
290 \placearrowat[green!50!yellow](6,5)(8,5)
291 \placerectangleat[red!25!black](8,4)(9,6)
292 \placefilledrectangleat[blue!25!red](8.5,4.5)(8.75,5.6)
293 \end{verbatim}
294
295 By now, it is not possible to specify an rgb code or similar. If you want a very special color that is not defined in the \textsc{xcolor} package, just define it by yourself. However, as shown above, it is possible to mix colors using the \verb|red!50!green| syntax, which is very flexible and should cover normal every day use.
296
297 \section{Example}
298 Now, here are two examples on how to use the package. The first one is a code example only, while the second one shows the effect directly on the page.
299
300 \subsection{Example use with beamer}
301 As this package makes most sense in combination with beamer, here is a small example about how to use it.
302 \begin{verbatim}
303 \documentclass[ngerman]{beamer}
304 \usepackage{babel,blindtext}
305 \usepackage{fontspec}
306 \usepackage{placeat}
307 \begin{document}
308 \begin{frame}{Test frame}
309 Test
310 \placeat<D5>{Test}
311 \placeminipageat{4}{5}[3cm]{\includegraphics{fermi_gas_1}}
312 \end{frame}
313 \end{document}
314 \end{verbatim}
315
316 \subsection{Example use inside this document}
317 The following page is typeset using the features of this package and shows the corresponding code.
318 \newpage
319 \placeatsetup{drawgrid, gridnumber=15}
320 However, this very page is using the \verb|drawgrid| option, with an increased grid number of 15. There are several elements placed with the given code, respectively.\footnote{Don't let me fool you, the code is not printed using \texttt{\textbackslash verb}, but only with a \texttt{\textbackslash texttt}, as verbatim is not possible with \texttt{\textbackslash placeat}.}
321 \placeat<F5>{\texttt{\textbackslash placeat<F5>{}}}
322 \placeat(4,7){\texttt{\textbackslash placeat(4.5,7.2){}}}
323 \placeat{2.3}{4.1}{\texttt{\textbackslash placeat\{2.3\}\{4.1\}{}}}
324 \placecircleat[blue](6,9)
325 \placeat(6.2,9){\texttt{\textbackslash placecircleat[blue](6,9)}}
326 \placearrowat[green](6,9)(8.5,5)
327 \placeat(7.5,7){\texttt{\textbackslash placearrowat[green](6,9)(8.5,5)}}
328 \placerectangleat[red](8,4)(9,6)
329 \placeat(9.2,5){\texttt{\textbackslash placerectangleat[red](8,4)(9,6)}}
330
331 \newpage
332 \section{How is it done?}
333 \placeatsetup{nogrid}
334 The short answer is: Look at the source code. While the coding is quite simple in principle, it might be very confusing when reading it, and I am still surprised it works at all …
335
336 Mainly, everything is based on the \LaTeX\ command \verb|\put(){}|. You could of course just use this, but then it's hard to get an absolute positioning as \verb|\put| only allows relative positions. You could then put your code into, say, a header line, and that is nearly the idea of this package. However, this would require a header and would not let the user freely decide what to put there. Also, users might do strange stuff to that and that could destroy the placing.
337
338 Instead, we use the ability of Heiko Oberdiek's \verb|atbegshi| package which adds content to the to-be-shipped-out-page. I still do not understand how it works, but it is absolutely robust and does just what we need here: It allows to put stuff on the page relative to, say, the upper right corner. Also, it can be put in front of every other thing, so we are sure nothing gets lost.
339
340 The next step is collecting and saving the material you specify to be placed somewhere. Collection is done using the \verb|xparse| package which allows for a very flexible macro definition which makes it possible to enter the different positioning options. Finally, everything is glued together with some Lua magic …
341
342 We save the content to be placed in \TeX\ macros that are numbered using a Lua counter; the final coordinates are also calculated by Lua. The \TeX-Lua interface is heavily used here which is possible due to the \verb|luacode| package. The macros are then executed in the call of \verb|\AtBeginShipout|, again inside a Lua loop, where also the grid is drawn.
343
344 \section{To Do}
345 A list of things I would like to have solved by some time:
346 \catcode`\•13
347 \let•\item
348 \begin{itemize}
349 • allow the |wave| color model as it is very very cool
350 • placing stuff at every page or reuse stuff at all
351 • allow course placing (put at upper left corner, put at left side etc.) for presentations
352 • verbatim in placeat
353 • drawing maybe based on metapost instead of pdf drawing
354 \end{itemize}
355
356 \section{How can I help?}
357 There are several ways how you can help. First, and most important:
358
359 Testing. Try to use this code and tell me what you think about it.
360
361 Bug reporting. Tell me especially what is buggy. I'd like to keep the package rather small and simple, so some bugs might be called features, but we'll see.
362
363 Suggestions. I'm open to extend the functionality. Just tell me what you want and I'll try to implement it as soon as possible. Which might be never. But also maybe the next day. Well, try it!~{\huge ☺}
364
365 \section{Thanks}
366 Of course I have to thank Paul Isambert again for the code for drawing to the pdf file. Also I want to thank Rembrandt Wolpert who was the first one to report bugs and feature requests.
367
368 \newpage
369 That's it for the documentation, have fun, and\vspace{2cm}
370
371 \placeat(5,7){\Huge\rotatebox{45}{Happy \TeX ing!}}[happy]
372 \placecircleat[red](4,4.5)(.2)
373 \placecircleat[green](9,4.5)(.2)
374 \placecircleat[blue](4,7.5)(.2)
375 \placecircleat[orange](9,7.5)(.2)
376
377 \begin{luacode}
378 for i = 1,20 do
379 tex.print("\\placecircleat[blue](12,6)("..(0.05*i)..")")
380 end
381 \end{luacode}
382
383
384 \DocInput{placeat.dtx}
385 \end{document}
386 %</driver>
387 %<*package>
388 % \fi
389 % \clearpage
390 %\part{Implementation}
391 %\label{sec:implementation}
392 %\section{The \LaTeX\ package: placeat.sty}
393 % Everything to get stuff working from the \TeX\ side. Here, only a .sty file is provided and plain/Con\TeX t users have to find their way. I'll happily support them, though!
394 % \subsection{Loading Files}
395 % The Lua file is not found by using a simple |dofile("placeat.lua")| call, but we have to use kpse's |find_file|.
396 % \begin{macrocode}
397 \ProvidesPackage{placeat}%
398 [2017/08/19 v0.1d1 absolute content positioning]
399 \RequirePackage{luatexbase}
400 \RequirePackage{luacode}
401 \RequirePackage{atbegshi}
402 \RequirePackage{xparse}
403 \directlua{dofile(kpse.find_file("placeat.lua",'lua'))}
404 % \end{macrocode}
405 % \subsection{User Commands}
406 % The main command \verb|\placeat|. There are several ways to use it, so we define a wrapper macro that is only for the user interface. Nice separation of interface and code. But actually, both are quite hard interwoven and it's not really clear at any time what happens. However, it works most of the time.
407 %
408 % The macro arguments of placeat at the moment are:\ \
409 % |g{}g{}|: two braced arguments for coordinates\\
410 % |d()|: one argument for picture-like coordinate pairs\\
411 % |d<>|: one argument for alpha-numeric coordinates\\
412 % |O{}|: content to be typeset on the left of the point\\
413 % |m|: main content to be typeset on the right.
414 % |o|: optional label for relative placement. This might now be the point to change the internal structure and go to a node mode.
415 % \begin{macrocode}
416 \NewDocumentCommand\placeat{ggd()d<>O{}mo}{
417 \IfValueT{#1}{ %% two coordinates in { }{ } pair.
418 \IfValueT{#2}{ %% if second argument is not given, everything breaks. not nice.
419 \def\cox{#1}
420 \def\coy{#2}
421 }
422 }
423 \IfValueT{#3}{ %% one argument as ( , ) coordinate pair.
424 \def\cox{\firstof#3X}
425 \def\coy{\secondof#3X}
426 }
427 \IfValueT{#4}{
428 \luadirect{
429 y = string.byte('#4',1)-64
430 x = string.byte('#4',2)-48
431 x2 = string.byte('#4',3)
432 if x2 then x = x*10 + x2-48 end -- FIXME: what exactly happens here? …
433 }
434 \def\cox{\luadirect{tex.print(x)}}
435 \def\coy{\luadirect{tex.print(y)}}
436 }
437 \placeatthreenumbers{\cox}{\coy}{\llap{#5}#6}
438
439 \IfValueT{#7}{
440 \expandafter\gdef\csname #7x\endcsname{\firstof#3X}
441 \expandafter\gdef\csname #7y\endcsname{\secondof#3X}
442 }
443 }
444 % \end{macrocode}
445 % \subsection{Relative Placement}
446 % The first stage of this works just the same as normal |\placeat|. However, there is an additional first optional argument that actually is \emph{not} optional! This is the node that is taken as base. So the |\placeatthreenumbers| is just called with the given coordinates added to the base coordinates.
447 % \begin{macrocode}
448 \NewDocumentCommand\placerelto{oggd()d<>O{}mo}{
449 \IfValueT{#2}{ %% two coordinates in { }{ } pair.
450 \IfValueT{#3}{ %% if second argument is not given, everything breaks. not nice.
451 \def\cox{#2}
452 \def\coy{#3}
453 }
454 }
455 \IfValueT{#4}{ %% one argument as ( , ) coordinate pair.
456 \def\cox{\firstof#4X}
457 \def\coy{\secondof#4X}
458 }
459 \IfValueT{#5}{
460 \luaexec{
461 y = string.byte('#5',1)-64
462 x = string.byte('#5',2)-48
463 x2 = string.byte('#5',3)
464 if x2 then x = x*10 + x2-48 end -- FIXME: what exactly happens here? …
465 tex.print("\\def\\cox{"..(x).."}\\def\\coy{"..(y).."}")
466 }
467 }
468 \placeatthreenumbers
469 {\cox + \csname #1x\endcsname}
470 {\coy + \csname #1y\endcsname}
471 {\llap{#6}#7}
472 \IfValueT{#8}{
473 \expandafter\xdef\csname #8x\endcsname{\cox + \csname #1x\endcsname}
474 \expandafter\xdef\csname #8y\endcsname{\coy + \csname #1y\endcsname}
475 }
476 }
477 % \end{macrocode}
478 % \subsection{Placing of floats etc.}
479 % For floats and similar stuff, it might be necessary or useful to pack everything into a minipage. You can do this by yourself, but I thought it might be nice to specify a corresponding user interface. Using \verb|\placeminipageat| is the same as using \verb|\placeat{}{}{content}| where content is packed into a minipage. The first two argument of \verb|\placeminipageat| must be given in braces |{4}{5}| and determine the position of the content. The third argument is optional and specifies the width of the minipage; if not give, it is assumed to be 10cm, wide enough for mostly anything you ever will place at.
480 % \begin{macrocode}
481 \NewDocumentCommand\placeminipageat{r()O{10cm}m}{
482 \gdef\widthofplaceat{#2}
483 \placeat(#1)
484 {\begin{minipage}{\widthofplaceat}{#3}\end{minipage}}
485 }
486 % \end{macrocode}
487 % \subsection{Helper Macros}
488 % The real stuff is done in the macro \verb|\placeatthreenumbers| which takes exactly three arguments defining the position of the content. The content is stored in a macro that is defined using Lua code, and the position is also calculated by Lua code. Everything is put together into a Lua-\TeX-bastard and surprisingly works stable as far as I can tell.
489 %
490 % This place is also where the offset and scaling happens.
491 % \begin{macrocode}
492 \def\placeatthreenumbers#1#2#3{
493 \luaexec{
494 nr = nr+1
495 dacoordtmp = ((#1-1+offsetx)*tex.pagewidth/65536/gridnrx*1.005)..","..(-(#2-1+offsety)*tex.pageheight/65536/gridnry)
496 dacoord[nr] = "\\put("..dacoordtmp..")"
497 tex.print("\\expandafter\\gdef\\csname command"..(nr).."\\endcsname")}% begin of command definition
498 {#3} %% this is what \command[nr] will contain
499 }
500 % \end{macrocode}
501 % Two tiny helpers that might be substituted by some standard commands:
502 % \begin{macrocode}
503 \def\firstof #1,#2X{#1}
504 \def\secondof #1,#2X{#2}
505 % \end{macrocode}
506 % Setup of variables and macros we need later.
507 % \begin{macrocode}
508 \gdef\drawgridnum{1}
509 \luaexec{
510 arrowheadlength = 5
511 drawgrid = false
512 nr = 0
513 dacoord = {}
514 gridnr = 10
515 gridnrx = 10
516 gridnry = 10
517 gridlinewidth = 0.01
518 offsetx = 0
519 offsety = 0
520 }
521 % \end{macrocode}
522 % Now the code that does the actual work here. We use Heiko Oberdiek's package \textsf{atbegshi} with the very useful macros \verb|\AtBeginShipout| and \verb|\AtBeginShipoutUpperLeftForeground|. Using these, we are free from any context of where the code is written, it is always executed at the shipout and therefore absolute positioning is possible.
523 %
524 % I have to use a quite weird way of checking wether to draw the grid or not, using a number instead of defining a \verb|\ifdrawgrid|. That one was working at some time, but now it is not anymore. No idea why, some handling of the input parsing in the arguments must have changed. Anyways, this is working and not too ugly, so we'll stick with that one for now.
525 % \begin{macrocode}
526 \AtBeginDocument{
527 \AtBeginShipout{%
528 \AtBeginShipoutUpperLeftForeground{%
529 \ifnum\drawgridnum = 1 \drawgrid\fi
530 \luaexec{%
531 for i = 1,nr do
532 tex.print(dacoord[i].."{\\csname command"..(i).."\\endcsname}")
533 end
534 nr=0
535 }
536 }
537 }
538 }
539 % \end{macrocode}
540 % \section{The Grid}
541 % The grid is made by drawing directly into the pdf as suggested by Paul Isambert in his TUGboat article “\textit{Drawing tables: Graphic fun with Lua\TeX}”. Labeling is done by simple \verb|\put| commands, controlled via Lua code.
542 % \begin{macrocode}
543 \def\drawgrid{
544 \luatexlatelua{
545 pdf_print("q")
546 linewidth(gridlinewidth)
547 local factorh = tex.pageheight/gridnry/65536
548 local factorw = tex.pagewidth/gridnrx/65536
549 for i = 1,math.max(gridnrx,gridnry) do
550 h = i*factorh
551 w = i*factorw
552 move(0,-h) line(tex.pagewidth,-h) stroke()
553 move(w,0) line(w,-tex.pageheight) stroke()
554 end
555 pdf_print("Q")
556 }
557 { %% extra grouping to keep font size change local. Going to normalfont seems to make sense. An explicit font for the grid might also be nice. Implementation only upon request.
558 %% would also be nice to maybe adapt the fontsize to the grid size
559 \normalfont\fontsize{8}{10}\selectfont
560 \luaexec{
561 for i=1,math.max(gridnrx+offsetx,gridnry+offsety) do
562 hfac = tex.pageheight/gridnry/65536
563 wfac = tex.pagewidth/gridnrx/65536*1.005 %% another empirical factor
564 h = (i-1)*hfac
565 w = (i-1)*wfac
566 tex.print("\\put("..(w)..",-7){\\rlap{"..(i-offsetx).."}}")
567 if alphanumgrid then
568 tex.print("\\put(0,"..(-h-0.05*hfac).."){\\char00"..(64+i-offsety).."}") %%-- for alphanumeric grid.
569 else
570 tex.print("\\put(0,"..(-h-0.05*hfac).."){"..(i-offsety).."}")
571 end
572 end
573 }
574 }
575 }
576 % \end{macrocode}
577 % \section{Drawing Stuff}
578 % Drawing is done in the same way as the grid. While the grid has no interface, the rest of the drawing stuff needs a \TeX\ interface, which is defined here. Every command calls a Lua function that does the actual work, as always.
579 %
580 % I try to provide a basic set of stuff that might be useful. The \TeX\ interface implementation might change, but for now it is done with xparse instead of a much more saner simple |\def|. We will see where this will head to. First, there is an arrow, whose head looks very bad. I don't know how to fix this yet. Then there are circle, square and rectangle.
581 % \begin{macrocode}
582 \NewDocumentCommand\placelineat{or()r()}{
583 \placeat(#2){\ignorespaces\IfValueT{#1}{\color{#1}} % only to fix the color!
584 \luatexlatelua{placelineat(#2,#3)}
585 }
586 }
587 \NewDocumentCommand\placearrowat{or()r()}{
588 \placeat(#2){\ignorespaces\IfValueT{#1}{\color{#1}}%
589 \luatexlatelua{placearrowat(#2,#3)}
590 }
591 }
592 \NewDocumentCommand\placecircleat{or()D(){.3}}{
593 \placeat(#2){\ignorespaces\IfValueT{#1}{\color{#1}}%
594 \luatexlatelua{placecircleat(#3,1)}
595 }
596 }
597 \NewDocumentCommand\placefilledcircleat{or()D(){.3}}{
598 \placeat(#2){\ignorespaces\IfValueT{#1}{\color{#1}}%
599 \luatexlatelua{placecircleat(#3,1,true)}
600 }
601 }
602 \NewDocumentCommand\placesquareat{or()G{3}}{
603 \placeat(#2){\ignorespaces\IfValueT{#1}{\color{#1}}%
604 \luatexlatelua{placesquareat(#3)}
605 }
606 }
607 \NewDocumentCommand\placecurveat{or()r()r()r()}{
608 \placeat(#2){\ignorespaces\IfValueT{#1}{\color{#1}}%
609 \luatexlatelua{placecurveat(#2,#3,#4,#5)}
610 }
611 }
612 \NewDocumentCommand\placerectangleat{O{black}r()d()}{
613 \placeat(#2){\ignorespaces\color{#1}%
614 \luatexlatelua{placerectangleat(#2,#3)}
615 }
616 }
617 \NewDocumentCommand\placefilledrectangleat{O{black}r()r()}{
618 \placeat(#2){\ignorespaces\color{#1}%
619 \luatexlatelua{placerectangleat(#2,#3,true)}
620 }
621 }
622 \NewDocumentCommand\placeroundedat{sO{black}r()D(){0.1}D<>{1.5}}{
623 \placeat(#3){\ignorespaces\color{#2}%
624 \IfBooleanTF{#1}{\luatexlatelua{placecircleat(#4,#5,true)}}%
625 {\luatexlatelua{placecircleat(#4,#5)}}
626 }
627 }
628 % \end{macrocode}
629 % \section{Key-Value Interface}
630 % It's a modern package, so we make use of \LaTeX3 once more. Let's see how stable this is. So far, no options can be used as package option, but only inside the \verb|\placeatsetup{}| macro. I'm not much into \LaTeX3 syntax and stuff anymore, so feel free to correct any non-nice coding here!
631 %
632 % Especially one thing will be annoying, the space-gobbling. Nice feature on one hand, but annoying inside the |\directlua| on the other hand. Therefore, we need the |~| to separate |gridnr| and |gridnry| below.
633 % \begin{macrocode}
634 \ExplSyntaxOn
635 \gdef\drawgridnum{1}
636 \keys_define:nn{placeat}{
637 alphanumgrid.code:n = \directlua{alphanumgrid = true},
638 arrowheadlength.code:n = \directlua{arrowheadlength=#1},
639 final.code:n = \luaexec{placeat_final = true} \gdef\drawgridnum{0},
640 drawgrid.code:n = \gdef\drawgridnum{1},
641 gridnumber.code:n = \directlua{gridnr = #1 gridnrx = gridnr~gridnry = gridnr},
642 gridnumberx.code:n = \directlua{gridnrx = #1},
643 gridnumbery.code:n = \directlua{gridnry = #1},
644 gridlinewidth.code:n = \directlua{gridlinewidth = #1},
645 linewidth.code:n = {\placeat(1,1){\luatexlatelua{linewidth(#1)}}}, %% FIXME: this is a very nasty hack to implement user's choice of linewidth!
646 nogrid.code:n = \gdef\drawgridnum{0},
647 numnumgrid.code:n = \directlua{alphanumgrid = false},
648 offsetx.code:n = \directlua{offsetx = #1},
649 offsety.code:n = \directlua{offsety = #1},
650 startzero.code:n = \directlua{offsetx = 1 offsety = 1}
651 }
652 \DeclareDocumentCommand\placeatsetup{m}{
653 \keys_set:nn{placeat}{#1}
654 }
655 \ExplSyntaxOff
656 % \end{macrocode}
657 %\iffalse
658 %</package>
659 %<*lua>
660 %\fi
661 % \section{Lua Module}
662 % \label{sec:luamodule}
663 % So far, the only usage of the Lua module is for graphics, based on the article by Paul Isambert about drawing directly to the pdf using Lua. We exploit this here and make use of the basic drawing functions he provided. Maybe this will be outsorced once there is a Lua-to-pdf-based graphics bundle.
664 % \begin{macrocode}
665 function pdf_print (...)
666 for _, str in ipairs({...}) do
667 pdf.print(str .. " ")
668 end
669 pdf.print("\n")
670 end
671
672 function move (p1,p2)
673 if (p2) then
674 pdf_print(p1,p2,"m")
675 else
676 pdf_print(p1[1],p1[2],"m")
677 end
678 end
679
680 function line(p1,p2)
681 pdf_print(p1,p2,"l")
682 end
683
684 function curve(p11,p12,p21,p22,p31,p32)
685 if (p22) then
686 p1,p2,p3 = {p11,p12},{p21,p22},{p31,p32}
687 else
688 p1,p2,p3 = p11,p12,p21
689 end
690 pdf_print(p1[1], p1[2],
691 p2[1], p2[2],
692 p3[1], p3[2], "c")
693 end
694
695 function linewidth(w)
696 pdf_print(w,"w")
697 end
698
699 function fill()
700 pdf_print("f")
701 end
702
703 function stroke()
704 pdf_print("S")
705 end
706
707 -- welp, let's have some fun!
708 -- with the function radd, a random coordinate change is added if used
709 -- randfact will adjust the amount of randomization
710 -- everything is relative in the grid size
711 -- BUT: In fact, do we really want to have wiggly lines? …
712 local randfact = 100
713 local radd = function()
714 return (math.random()-0.5)*randfact
715 end
716
717 function placelineat(x1,y1,x2,y2)
718 xfac = tex.pagewidth/gridnrx/65536 -- factors to convert given number to absolute coordinates
719 yfac = tex.pageheight/gridnry/65536 -- should both be global!
720 xar = (x2-x1)*xfac -- end point of the arrow
721 yar = (y1-y2)*yfac --
722 move(0,0) -- start
723 line(xar,yar) -- draw main line
724 stroke()
725 end
726
727 function placearrowat(x1,y1,x2,y2)
728 xfac = tex.pagewidth/gridnrx/65536 -- factors to convert given number to absolute coordinates
729 yfac = tex.pageheight/gridnry/65536 -- should both be global!
730 xar = (x2-x1)*xfac -- end point of the arrow
731 yar = (y1-y2)*yfac --
732 parx = xar/math.sqrt(xar^2+yar^2) -- direction of the arrow
733 pary = yar/math.sqrt(xar^2+yar^2) --
734 perpx = -pary -- perp of the arrow direction
735 perpy = parx --
736 move(0,0) -- start
737 line(xar,yar) -- draw main line
738 move(xar,yar)
739 line(xar-arrowheadlength*parx+arrowheadlength*perpx,yar-arrowheadlength*pary+arrowheadlength*perpy) -- draw arrowhead
740 move(xar,yar)
741 line(xar-arrowheadlength*parx-arrowheadlength*perpx,yar-arrowheadlength*pary-arrowheadlength*perpy)
742 stroke()
743 end
744
745 -- better circle-approximation by using quarter circles, according to wikipedia article about Bézier curves
746 -- k = 1 gives a circle, everything else something else …
747 function placecircleat(r,k,filled)
748 local P0,P1,P2,P3
749 r = r * 59.5 -- next arbitrary scale factor; the circle has radius "1" in x-units
750 local rk = 0.55228*r*k
751
752 P0 = {r,0}
753 move (P0[1],P0[2])
754
755 P1 = {r,rk} P2 = {rk,r} P3 = {0,r}
756 curve (P1,P2,P3)
757
758 P1 = {-rk,r} P2 = {-r,rk} P3 = {-r,0}
759 curve (P1,P2,P3)
760
761 P1 = {-r,-rk} P2 = {-rk,-r} P3 = {0,-r}
762 curve (P1,P2,P3)
763
764 P1 = {rk,-r} P2 = {r,-rk} P3 = {r,0}
765 curve (P1,P2,P3)
766
767 if filled then
768 fill()
769 end
770 stroke()
771 end
772
773 function placesquareat(length)
774 move (-length,-length)
775 line ( length,-length)
776 line ( length, length)
777 line (-length, length)
778 line (-length,-length)
779 stroke()
780 end
781
782 function placecurveat(x1,y1,x2,y2,x3,y3,x4,y4) -- start point and three numbers. Start is only offset.
783 xfac = tex.pagewidth/gridnrx/65536 -- factors to convert given number to absolute coordinates
784 yfac = tex.pageheight/gridnry/65536 -- should both be global!
785 x2 = (x2-x1)*xfac
786 y2 = (y2-y1)*yfac
787 x3 = (x3-x1)*xfac
788 y3 = (y3-y1)*yfac
789 x4 = (x4-x1)*xfac
790 y4 = (y4-y1)*yfac
791 move(0,0) -- start
792 curve(x2,-y2,x3,-y3,x4,-y4) -- coordinates for Bezier curve
793 stroke()
794 end
795
796 function placerectangleat(x1,y1,x2,y2,filled)
797 xfac = tex.pagewidth/gridnrx/65536
798 yfac = tex.pageheight/gridnry/65536
799 x2 = (x2-x1)*xfac
800 y2 = (y1-y2)*yfac
801 move(0,0)
802 line(x2,0)
803 line(x2,y2)
804 line(0,y2)
805 line(0,0)
806 if filled then
807 fill()
808 end
809 stroke()
810 end
811 % \end{macrocode}
812 % \Finale
813 % \endinput