[NTG-context] [m-database] How to properly add module arguments?

Paul Mazaitis paul at mazaitis.org
Sun Feb 26 01:52:18 CET 2023

Dear list,

## The Incredibly Short Version

I am trying to extend m-database, and I want to add a flag to control behavior. I can get it to work, but I am having trouble understanding how to do it properly.

## The Short Version

I have a project where I wish to incorporate many CSV files as Natural Tables.

I want to keep the headers in the CSV files so they're useful in other contexts.

The `m-database` module doesn't have a feature to suppress the header row, from what I can see.

(I know `handlecsv` can do this, but I'm having troubles with that; if the `m-database` line of inquiry doesn't work out, I'll go back it, but I figure I'll focus on this solution for now.)

Looking at the module sources, I can implement the functionality I need in the Lua part of the module, but:

I seem to only be able to bodge in additional arguments for the commands in the module.

On first try, I got the error: `tex error       > tex error on line 30 in file /Users/pmazaitis/Opt/context/tex/texmf-context/tex/context/modules/mkiv/m-databaseplus.mkiv: ! Undefined control sequence`

And then: `The control sequence at the end of the top line of your error message was never

Looking at the docs points me to a solution using  `\def`, but it's not clear to me how that's working in the case of the database module (the database module seems to be initializing these values someplace else?).

If I \def the settings variable in m-database.mkiv,  I can get it to work, but I don't think that's the correct way to go about this.

Question: how do I properly define new module arguments for the database module?

## The Long Version, With MWE

### Files Associated With the MWE

Apologies for all of the file inclusions, but I want to be complete.

Here's the .csv file I'm using for the MWE:

--- begin database-mwe.csv ---

Ref. No.,Part No.,Description,Remarks
,RD14C82E000J,Carbon 000𝝮 ±5% 1/4W,

---  end database-mwe.csv  ---

Some notes on database-mwe.csv:

I'd like to keep the header row in place to make this file useful for other tools.

These are lists of electronic parts, so I'd like to include uppercase omega, lower case mu, and percent sign characters in the CSV file.

Here's the .tex file I'm using for the MWE:

--- begin database-mwe.tex ---



    \bTH Ref. No. \eTH \bTH Part No. \eTH \bTH Description \eTH \bTH Remarks \eTH





---  end database-mwe.tex  ---

Some notes on database-mwe.tex:

This actually works pretty well with the distributed m-database module!

Some of these lists are quite long, so I want to use a TABLEhead to replicate the headings at the top of a new page. I don't think there's a way to automate this with `m-database`, so I'm okay with hard-coding the headers. This does mean I get a double header row at the top of the table. I want to suppress the header row; I'd like to implement this behavior in the module behind a switch.

(I'm using the `asciimode` environment to dodge problems with unescaped `%` characters and Greek letters. I tried adding the startasciimode/stopasciimode commands to various keys in the \defineseparatedlist command, but I didn't get anywhere with it - I got errors. A problem for another time...)

I think I've got a good handle on how to do this in Lua, but I'm struggling with what to add to the switch appropriately.

In attempting to add a switch, I tried the following.

Based off of https://wiki.contextgarden.net/Modules#Modules_writing_guidelines,
I added a stanza to the appropriate interface file:

--- begin stanza added to i-database.xml ---

                <cd:parameter name="skipheader">
                    <cd:constant type="yes"/>
                    <cd:constant type="no" default="yes"/>

---  end stanza added to i-database.xml  ---

I modeled that after the stanza for the argument `strip`. I think that's the only thing I need to do in the interface file.

Changing the i-database.xml file does not break the build.

However, as soon as I add the skipheader argument to the m-database.mkiv file:

--- begin definition in m-database.mkiv ---

   \c!skipheader=\v!no, % <- Added, following the pattern with strip

---  end definition in m-database.mkiv  ---

...the build fails (with the error from the Short Version section).

If I then add the following \def to m-database.mkiv before the \setupdatabase command:

    \def\c!skipheader {skipheader}

...the build succeeds! I can now pass that settings value to m-database.lua and use it to modify the processing logic.

My question: how do I \emph{properly} define new module arguments for the database module?

Or: is adding extra \def commands the way to do it?

Many, many thanks!


More information about the ntg-context mailing list