LATEX3: Usingthe layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3

LATEX3:Using the layers

Conclusions

It’s alright ma, it’s only witchcraftFrank Mittelbach & Joseph Wright2013-10-24

LATEX3: Usingthe layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions

Himeji 2013

Outline
LATEX3: Usingthe layers
FrankMittelbach &Joseph Wright

1Layers2

Creating the interface layer: xparse3

Code layer:

LayersCreating theinterface layer:
xparseCode layer:expl3
Conclusions

LATEX3 layers
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayersCreating theinterface layer:
xparseCode layer:
expl3
Conclusions

Layers of abstraction
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayersCreating theinterface layer:
xparseCode layer:
expl3
Conclusions

Code Data structures and commands to build
higher-level typesetting elements
Functionality Typesetting elements that can be customized toshow varying behaviours
Design Specific elements and formatting from thefunctionality layer
User User level syntax to call instances

The LATEX 2ε situation
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions

User \section * [hTOC i] {hheadingi}
Design Use of \@startsection:
sets appearance of sections
Functionality Arguments of \@startsection:e.g. vertical space above below, etc.
Code \if@noskipsec \leavevmode \fi\addpenalty...

The design layer
Describing design
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions

Ideally, document design could be written independent of code
Section headers will be set in 16 point sanserif,
with space before the section of 6 points and after of 6points unless immediately followed by a subsection inwhich case . . .
Some ideas on this problem in xtemplate plus the ‘LATEX database’: these are difficult problems!

The functionality layer
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3Conclusions\def\@startsection#1#2#3#4#5#6{%\if@noskipsec \leavevmode \fi\par\@tempskipa #4\relax\@afterindenttrue\ifdim \@tempskipa <\z@\@tempskipa -\@tempskipa \@afterindentfalse\fi\if@nobreak\everypar{}%\else\addpenalty\@secpenalty\addvspace\@tempskipa\fi\@ifstar{\@ssect{#3}{#4}{#5}{#6}}%{\@dblarg{\@sect{#1}{#2}{#3}{#4}{#5}{#6}}}}

The code layer
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions

Where do you find documentation for each of the following?
Which can you use in your own code?
\def\@float#1{%\@ifnextchar[%{\@xfloat{#1}}%{\edef\reserved@a{\noexpand\@xfloat{#1}[\csname fps@#1\endcsname]}%\reserved@a}}\def\@dblfloat{%\if@twocolumn\let\reserved@a\@dbflt\else\let\reserved@a\@float\fi\reserved@a}\def\@xfloat #1[#2]{%\@nodocument\def \@captype {#1}%...

Outline
LATEX3: Using
the layers
Frank
Mittelbach &Joseph Wright

1Layers2

Creating the interface layer: xparse3

Code layer:

LayersCreating theinterface layer:
xparseCode layer:
expl3
Conclusions

\newcommand in LATEX 2εWith a star . . .
LATEX3: Usingthe layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3\newcommand*{\foo}{Code with no arguments}\newcommand*{\foo}[2]{Code using #1 and #2}\newcommand*{\foo}[2][]{Code using #1 and #2}

Conclusions\newcommand*{\foo}[2][default]{Code using #1 and #2}

\newcommand in LATEX 2ε. . . or withoutLATEX3: Usingthe layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3\newcommand {\foo}{Code with no arguments}\newcommand {\foo}[2]{Code using #1 and #2}\newcommand {\foo}[2][]{Code using #1 and #2}

Conclusions\newcommand {\foo}[2][default]{Code using #1 and #2}

The aims of xparse
LATEX3: Using
the layers
FrankMittelbach &Joseph Wright

Separate syntax from functionality
LayersCreating theinterface layer:
xparseDefine all arguments in one place
Intersperse mandatory and optional arguments

Code layer:
expl3

More types of argument without code

Conclusions

Mix long and short arguments
Create engine robust commands
Informative error messages

\...DocumentCommand
LATEX3: Using
the layers
FrankMittelbach &Joseph Wright\NewDocumentCommand

Layers\RenewDocumentCommand

Creating theinterface layer:
xparse\ProvideDocumentCommand

Code layer:
expl3
Conclusions\DeclareDocumentCommand\...DocumentCommand
LATEX3: Using
the layers
Frank
Mittelbach &Joseph Wright\NewDocumentCommand

Layers\RenewDocumentCommand

Creating theinterface layer:
xparse\ProvideDocumentCommand

Code layer:
expl3\DeclareDocumentCommand

Conclusions

Syntax\DeclareDocumentCommand {\hcommand i}{harg. spec.i} {hcodei}

Mandatory arguments
LATEX3: Usingthe layers
FrankMittelbach &Joseph Wright\DeclareDocumentCommand{\foo}{ }{Code using no arguments}

LayersCreating theinterface layer:
xparse\DeclareDocumentCommand{\foo}{ m }{Code using #1}

Code layer:
expl3
Conclusions\DeclareDocumentCommand{\foo}{ m m }{Code using #1 and #2}\DeclareDocumentCommand{\foo}{ m m m }{Code using #1, #2 and #3}

Mandatory arguments
Mixing short and long
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3\DeclareDocumentCommand{\foo}{ m m m }{Three short arguments}\DeclareDocumentCommand{\foo}{ +m +m +m }{Three long arguments}

Conclusions\DeclareDocumentCommand{\foo}{ m +m m }{Only #2 is long}Optional arguments(Almost) like LATEX 2εLATEX3: Using
the layersFrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions\DeclareDocumentCommand{\foo}{ O{} }{One optional argument}\DeclareDocumentCommand{\foo}{ O{default} m }{First argument optional with default value}Optional arguments
Beyond LATEX 2εLATEX3: Usingthe layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions\DeclareDocumentCommand{\foo}{ O{} O{} m }{Two optionals then a mandatory}\DeclareDocumentCommand{\foo}{ o m }{%\IfNoValueTF{#1}%{Code for just #2}%
{Code for #1 and #2}%}

Optional arguments
Beyond LATEX 2εLATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3\newcommand*{\foo}[2][]{Code here}\foo[\baz[arg1]]{arg2}

Conclusions#1 = \baz[arg1#2 = ]

Optional arguments
Beyond LATEX 2εLATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayersCreating theinterface layer:
xparseCode layer:
expl3\DeclareDocumentCommand{\foo}{ O{} m }{Code here}\foo[\baz[arg1]]{arg2}

Conclusions#1 = \baz[arg1]#2 = arg2

StarsLATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions\DeclareDocumentCommand{\foo}{ s m }{%\IfBooleanTF {#1}%{Process #2 with a star}%{Process #2 without a star}%}Re-implementing \chapter
The original
LATEX3: Using
the layers
FrankMittelbach &Joseph Wright\def\chapter{...\secdef\@chapter\@schapter}

LayersCreating theinterface layer:
xparse% syntax support code:

Code layer:
expl3\def\secdef#1#2{\@ifstar{#2}{\@dblarg{#1}}}\def\@ifstar#1{\@ifnextchar *{\@firstoftwo{#1}}}\long\def\@dblarg#1%{\kernel@ifnextchar[{#1}{\@xdblarg{#1}}}\long\def\@xdblarg#1#2{#1[{#2}]{#2}}

Conclusions

Re-implementing \chapter
In xparse
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions\ExplSyntaxOn % So we don’t worry about spaces!
\DeclareDocumentCommand { \chapter } { s o m }{\IfBooleanTF {#1}
{ \@schapter {#3} } % ignore [..] if given{ \IfNoValueTF {#2}
{ \@chapter [#3] {#3} } % use title twice{ \@chapter [#2] {#3} } % use [..] & title}}

Other argument types
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3Conclusions

d An optional argument delimited by two tokens
g An optional argument in braces (‘group’)r A mandatory argument delimited by two tokens (‘required’)t A single optional token
v An argument read verbatim

Outline
LATEX3: Using
the layers
FrankMittelbach &Joseph Wright

1Layers2

Creating the interface layer: xparse3

Code layer:

LayersCreating theinterface layer:
xparseCode layer:
expl3
Conclusions

Aims
LATEX3: Using
the layers
Frank
Mittelbach &Joseph WrightLayersCreating theinterface layer:
xparseA complete coding environment
Extensive set of basic and complex data types andcontrol structures
Consistent interfaces

Code layer:
expl3

Fully documented

Conclusions

Rigorously tested
Clear guidance on what is usable‘Best practice’ promoted by team...

A crash course in expl3
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions

Coding is done inside \ExplSyntaxOn. . . \ExplSyntaxOff: and _ are used systematically in names
White space is ignored
Separation of commands for internal use from thosepublicly-available (documented)
Argument preprocessing instead of expansion based
programming

Not much like TEX, is it?
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions\cs_new_protected:Npn \malmedal_output_reverse:{\seq_set_eq:NN \l__malmedal_temp_seq\g__malmedal_input_seq\seq_reverse:N \l__malmedal_temp_seq\int_set:Nn \l__malmedal_count_int{ \seq_count:N \l__malmedal_temp_seq }\seq_map_inline:Nn \l__malmedal_temp_seq{\__malmedal_print:n {##1}\int_decr:N \l__malmedal_count_int}}

Not much like TEX, is it?
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions%<@@=malmedal>\cs_new_protected:Npn \malmedal_output_reverse:{\seq_set_eq:NN \l_@@_temp_seq\g_@@_input_seq\seq_reverse:N \l_@@_temp_seq\int_set:Nn \l_@@_count_int{ \seq_count:N \l_@@_temp_seq }\seq_map_inline:Nn \l_@@_temp_seq{\@@_print:n {##1}\int_decr:N \l_@@_count_int}}

Argument preprocessing
instead of expansion
LATEX3: Using
the layers

LATEX 2ε code fragment for font selection:

FrankMittelbach &Joseph Wright\expandafter\in@\csname sym#3%\expandafter\endcsname\expandafter{\group@list}%...
\fiLayers
Creating theinterface layer:
xparseCode layer:
expl3Conclusions

Argument preprocessing
instead of expansion
LATEX3: Using
the layers

LATEX 2ε code fragment for font selection:

FrankMittelbach &Joseph Wright\expandafter\in@\csname sym#3%\expandafter\endcsname\expandafter{\group@list}%...
\fiLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions

How it could look like in expl3:\seq_test_in:cVTF { sym #3 } \l_group_seq{ true code } { false code }So you want to learn expl3?
LATEX3: Usingthe layers
Frank
Mittelbach &Joseph Wright

Read the documentation:
LayersCreating theinterface layer:
xparseCode layer:
expl3
Conclusions

expl3
interface3

Blog posts on texdev.net:
search for ‘Programming LaTeX3’
Ask questions on LaTeX-L or TeX-sx!

A lot done . . . usable . . . and used
LATEX3: Usingthe layers
FrankMittelbach &Joseph Wright

Code layer (expl3) works and is ‘out there’
xparse is much more powerful than \newcommandLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions

Using these allows implementation of new ideas for
LATEX3, e.g., xcoffins
But also packages explicitly for LATEX 2ε such as
fontspec by Will Robertson
chemmacros by Clemens Niederberger
Media9 by Alexander Grahn
xpatch by Enrico Gregorio...

. . . a lot left to do!
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseDesign layer is still to be solved
The output routine is a major challenge

Code layer:
expl3

There is always more to add to expl3

Conclusions

Choosing what not to add to expl3 is also hard!

Himeji Castle 2013 under renovation
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayers
Creating theinterface layer:
xparseCode layer:
expl3
Conclusions

Himeji 2013

Himeji Castle 2014/15?
LATEX3: Using
the layers
FrankMittelbach &Joseph WrightLayersCreating theinterface layer:
xparseCode layer:
expl3
Conclusions

Picture by Arthena (2008)