I have done something similar 

in Document -> Settings -> Local Layout 

[...]
Style EDD
   CopyStyle Date
   LabelFont
       Series Bold
       Color Red
   EndFont
   LatexName renewcommand
   LatexParam {\LMP}
   LabelString "LMP:"
   Preamble
     \newcommand\LMP[1]{#1}
   EndPreamble
End
[...]

and this in Document -> Settings -> LaTeX Preamble

[...]
usepackage{luacode}
\begin{luacode*}
function gbt(refdat)
   local year, month, day = refdat:match("(%d%d%d%d)-(%d%d)-(%d%d)")
   local gebtermin = os.time({day = day + 283, month = month, 
         year = year})
   local t = os.date("!%Y-%m-%d",gebtermin)
   return t
end
function hpv(refdat)
   local year, month, day = refdat:match("(%d%d%d%d)-(%d%d)-(%d%d)")
   local w24 = os.time({day = day + 168, month = month, year = year})
   local VersEnde = os.date("*t", w24)
   local wdshift = {-2,-3,3,2,1,0,-1}
   local sh = wdshift[VersEnde.wday]
   local korrhpv = os.time({day = VersEnde.day + sh, 
         month = VersEnde.month, year = VersEnde.year})
   local v = os.date("!%Y-%m-%d",korrhpv)
   return v
end
\end{luacode*}
\newcommand\EDD[1]{\directlua{tex.sprint(gbt("#1"))}}
\newcommand\CUTOFF[1]{\directlua{tex.sprint(hpv("#1"))}}
[...]

results in this LaTeX Code

[...]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\newcommand\LMP[1]{#1}
[...]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{luacode}
[...]
\newcommand\EDD[1]{\directlua{tex.sprint(gbt("#1"))}}
\newcommand\CUTOFF[1]{\directlua{tex.sprint(hpv("#1"))}}
[...]

which as you can imagine allows me to put the Date of Last Menstruation
in ISO format in the Red LMP: field in the LyX GUI and at PDF generation
\EDD{\LMP} makes the Expected Date of Delivery appear automatically
whereas \CUTOFF{\LMP} produces the Friday nearest to 24 weeks of
pregnancy.

This (accessing LUA (or even knitR) stuff from the GUI, for example) can
become extremely powerful :-)-O

el


On 26/02/2019 09:16, Kornel Benko wrote:
> Am Dienstag, 26. Februar 2019 01:35:52 CET schrieb Christian 
> <cde...@gmail.com>:
>> Sorry, but I don't see how this solves the problem.  This solution
>> assumes that the variable allocation is always fixed, so that loading
>> the module would be equivalent to have a set of variables
>> automatically allocated by "% some stuff to go in the preamble".
>> This is not what I was aiming for.  The idea is to create some LyX
>> layout (environment, or similar, like "Author", or "Abstract").  Is
>> it possible to do it?
> 
> It is exactly as Riki posted.  Look for example in
> acmsiggraph-0-92.layout Style "Copyright_year"
> 
>       Kornel

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to