5

The examples for xtemplate show how to make document commands, but I haven’t seen anything involving environments.

If I were to define an environment using xtemplate, what would the best approach be? The two ways I immediately think of are:

  1. Define a single object, env, and two templates, start and end.
  2. Define two objects, env-start and env-end.

The way xtemplate works gives me the impression that it’s focused on commands only; is that the case?

2
  • Welcome to TeX.SX!
    – stone-zeng
    Commented Sep 25, 2018 at 3:24
  • I think there is nothing about "making document commands" in xtemplate. This package provides commands like UseInstance, which can be used for creating both commands and environments. "Creating things" itself, should be done via xparse.
    – stone-zeng
    Commented Sep 25, 2018 at 3:32

1 Answer 1

4

As mentioned in the comments, you don't directly define either commands or environments with xtemplate. You probably want to use xparse for that. Although I suppose you logically could define a separate template or instance for the ending part, I'm having trouble envisioning a use case for that. When are you going to need to have different parameters for the beginning and and of the environment?

The way I use template instances within environments is to have the code in \DeclareTemplateCode handle the initialization at the beginning of the environment and then create a separate function to handle the closing. Then your environment defined with xparse can just call those two functions, which is in keeping with the expl3 philosophy of keeping the user interface separate from the coding layer.

Here's an illustration:

\documentclass{article}
\usepackage{xparse,xtemplate}
\usepackage{lineno}
\usepackage{lipsum}

\ExplSyntaxOn

\DeclareObjectType{test}{0}

\DeclareTemplateInterface{test}{default}{0}
{
  linenum-modulus : code = 5
}

\DeclareTemplateCode{test}{default}{0}
{
  linenum-modulus = \modulolinenumbers[#1]
}
{
  \AssignTemplateKeys
  % just something for illustration
  \linenumbers
}

\cs_new_protected:Nn \smith_testme_stop:
{
  % Do cleanup here
}

\DeclareInstance{test}{first}{default}{}

\NewDocumentEnvironment{testme}{}
{
  \UseInstance{test}{first}
}
{
  \smith_testme_stop:  
}

\ExplSyntaxOff

\begin{document}

\begin{testme}
  \lipsum[1]
\end{testme}

\lipsum[2]

\end{document} 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .