Generalexpl3 for dummies

LaTeX specific issues not fitting into one of the other forums of this category.
erwann
Posts: 75
Joined: Thu Aug 25, 2016 2:24 am

expl3 for dummies

Post by erwann »

I would I rewrite this code inside a package (*.dtx) using expl3?

Code: Select all

\def\the@cartoon
	{
		Betty Boop
	}
x_86 / Linux Mint 18.3 / texlive 2015.20160320-1ubuntu0.1 / TeXworks 0.5r1361 (Debian)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10340
Joined: Mon Mar 10, 2008 9:44 pm

expl3 for dummies

Post by Stefan Kottwitz »

For example:

Code: Select all

\tl_const:Nn \c_the_cartoon {Betty\ Boop}
Test example:

Code: Select all

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn

\tl_const:Nn \c_the_cartoon {Betty\ Boop}

\NewDocumentCommand { \thecartoon } { }
  {
    \tl_use:N \c_the_cartoon
  }
  

\ExplSyntaxOff
\begin{document}
\thecartoon
\end{document}
Stefan
LaTeX.org admin
erwann
Posts: 75
Joined: Thu Aug 25, 2016 2:24 am

expl3 for dummies

Post by erwann »

Would it be possible to have an example showing how '\use:n' would come in handy? (page 16 of interface3.pdf)
x_86 / Linux Mint 18.3 / texlive 2015.20160320-1ubuntu0.1 / TeXworks 0.5r1361 (Debian)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

expl3 for dummies

Post by cgnieder »

erwann wrote:I would I rewrite this code inside a package (*.dtx) using expl3?

Code: Select all

\def\the@cartoon
	{
		Betty Boop
	}
That depends. Is \the@cartoon going to be a tokenlist that is never going to change? Or may it be that its value (definition) changes (maybe depending on user settings)?

Code: Select all

% constant public variable:
\tl_const:Nn \c_mypackage_cartoon_tl {Betty~ Boop}

% constant internal variable:
\tl_const:Nn \c__mypackage_cartoon_tl {Betty~ Boop}

% public variable:
\tl_new:N \l_mypackage_cartoon_tl
\tl_set:N \l_mypackage_cartoon_tl {Betty~ Boop}

% internal variable:
\tl_new:N \l__mypackage_cartoon_tl
\tl_set:N \l__mypackage_cartoon_tl {Betty~ Boop}
I have written a fes packages with expl3 (have a look at the source codes of chemmacros, chemformula, leadsheets or xsim for example) and have barely ever used \use:n.

It is hard to give more than general advice on such a general question. It is much easier to give example on how to solve concrete problems.
site moderator & package author
erwann
Posts: 75
Joined: Thu Aug 25, 2016 2:24 am

expl3 for dummies

Post by erwann »

Thank you for all the feedback. These guideline conforming macro names are long, is there not such a thing similar to namespaces?
x_86 / Linux Mint 18.3 / texlive 2015.20160320-1ubuntu0.1 / TeXworks 0.5r1361 (Debian)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

expl3 for dummies

Post by cgnieder »

No in TeX/LaTeX there is no namespace. This is part of the reason for the naming convention. The other reason is readability. After getting accustomed to the convention the code is far easier to read and understand than “traditional” (La)TeX code.
site moderator & package author
erwann
Posts: 75
Joined: Thu Aug 25, 2016 2:24 am

expl3 for dummies

Post by erwann »

Two undescores for internal, one for public TL's. ExplSyntaxOn/Off inside 'document' environment seems odd, or I am missing something?
x_86 / Linux Mint 18.3 / texlive 2015.20160320-1ubuntu0.1 / TeXworks 0.5r1361 (Debian)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

expl3 for dummies

Post by cgnieder »

erwann wrote:Two undescores for internal, one for public TL's.
Not only tokenlists but all kinds of variables, even functions. Public means that those variables and functions are allowed to be used by others and ideally are documented (just like all those in interface3). Internal functions and variables are not meant to be used by anyone (which also means there is no need to document them):

Code: Select all

\l_module_name_tl % local public tokenlist
\l__module_name_tl % local internal token list
\g_module_name_tl % global public tokenlist
\g__module_name_tl % global internal token list
\l_module_name_int % local public integer
\l__module_name_int % local internal integer
...
\module_name:n % public function with one brace group argument
\__module_name:n % internal function with one brace group argument
erwann wrote:ExplSyntaxOn/Off inside 'document' environment seems odd, or I am missing something?
Quite true. I never do that in my real world documents. Only in the preamble or in packages.
site moderator & package author
erwann
Posts: 75
Joined: Thu Aug 25, 2016 2:24 am

expl3 for dummies

Post by erwann »

Funny that the last response is attributed to someone other than myself.
Screen Shot 2018-03-15 at 7.37.08 PM.png
Screen Shot 2018-03-15 at 7.37.08 PM.png (24.56 KiB) Viewed 12194 times
x_86 / Linux Mint 18.3 / texlive 2015.20160320-1ubuntu0.1 / TeXworks 0.5r1361 (Debian)
erwann
Posts: 75
Joined: Thu Aug 25, 2016 2:24 am

expl3 for dummies

Post by erwann »

I would need, if possible, some clarification as to what the intended purposes of local public, local internal, global public, and public function are.

Code: Select all

\l_module_name_tl % local public tokenlist
    \l__module_name_tl % local internal token list
    \g_module_name_tl % global public tokenlist
    \g__module_name_tl % global internal token list
    \l_module_name_int % local public integer
    \l__module_name_int % local internal integer
    ...
    \module_name:n % public function with one brace group argument
    \__module_name:n % internal function with one brace group argument
x_86 / Linux Mint 18.3 / texlive 2015.20160320-1ubuntu0.1 / TeXworks 0.5r1361 (Debian)
Post Reply