New answers tagged

0 votes

Get string from withing curly brackets

If I had to do it on a traditional engine without the extensions of LuaTeX or eTeX and without expl3 programming layer, maybe I would do it like this: \makeatletter %%==================================...
Ulrich Diez's user avatar
  • 28.1k
0 votes

Get string from withing curly brackets

The text can be extracted with \regex_extract_once:nVN and the regular expression \{(.+)\}. Then the desired text is given by the second item in the sequence \l_tmpa_seq. \documentclass[border=6pt]{...
matexmatics's user avatar
  • 3,619
0 votes

Compare first token after expanding macro once and discard the rest

The gist of this answer is what is provided in wipet's answer, where \futurelet is used. However, a slight modification could be: If expandability is not needed and you don't like to work with ...
Ulrich Diez's user avatar
  • 28.1k
0 votes

Unexpected behavior of \g@addto@macro in class definition

Just as un update, I created a working MWE, that is based on the help of @Ulrich Diez. I thank him for the suggestion that made the code working. In the following days I would also try the solution by ...
TrAmA's user avatar
  • 15
2 votes

Fallback if def failed to parse the arguments?

Perhaps slower, but definitely not harder to write. \documentclass{article} \ExplSyntaxOn \NewDocumentCommand{\firstTry}{} { \peek_regex_replace_once:nnF { ([^\c{par}]*?) MYSEP ([^\c{par}]*?) ...
egreg's user avatar
  • 1.1m
2 votes

Fallback if def failed to parse the arguments?

How about: \documentclass{article} \newcommand\UDfirstoftwo[2]{#1} \newcommand\UDsecondoftwo[2]{#2} \csname @ifdefinable\endcsname{\firstTry}{% \long\def\firstTry#1EOL{\firstTryA#1SEPSEPEOL{#1}}% }...
Ulrich Diez's user avatar
  • 28.1k
2 votes

Unexpected behavior of \g@addto@macro in class definition

Instead of bothering with expansion you can simply use a key=value parser which doesn't store the values inside of macros, but instead forwards them as separate parameters to another macro. This is ...
Skillmon's user avatar
  • 59.3k
1 vote
Accepted

Unexpected behavior of \g@addto@macro in class definition

When asking a question where bug-tracking and modifying code plays a rôle always provide a minimal reproducible example (MRE)/minimal complete and verifiable example (MCVE) so that one can test ...
Ulrich Diez's user avatar
  • 28.1k
4 votes

Compare first token after expanding macro once and discard the rest

You can use \futurelet TeX primitive after the argument is expanded once by the \expandafter chain: \def\startswithtokenmymacro#1{% \expandafter\futurelet\expandafter\next\expandafter\swtmmA#1\...
wipet's user avatar
  • 73.3k
1 vote

Compare first token after expanding macro once and discard the rest

(In most situations in practical life expandability and a sentinel token being placed behind the last token of the argument is not an issue. So in practical life wipet's answer definitely is to be ...
Ulrich Diez's user avatar
  • 28.1k
4 votes
Accepted

Compare first token after expanding macro once and discard the rest

You've actually described the steps pretty well yourself. The only thing missing is the code implementing them :-) \documentclass{article} \def\mymacro#1{Does something with #1} \def\foo{\mymacro{1}} ...
campa's user avatar
  • 30.8k
6 votes
Accepted

Fallback if def failed to parse the arguments?

Use appropriate delimited macros \documentclass{article} \def\firstTry#1EOL{\firstTryA#1SEPSEPEOL} \def\firstTryA#1SEP#2SEP#3EOL{% I want to extract ``\ifx&#2&#1\else#2\fi''% } \begin{...
campa's user avatar
  • 30.8k
2 votes

Get third argument from string?

In case there is a practical use case scenario behind this and not just academical interest: The concept of "string" is vague in TeX. What kind of tokens may the string contain? How do you ...
Ulrich Diez's user avatar
  • 28.1k
6 votes

Get third argument from string?

Using a bunch of \expandafter tokens might be fun, but there are much better ways now than \@car and \@cdr. \documentclass{article} \ExplSyntaxOn \NewExpandableDocumentCommand{\firstof}{m} { %\...
egreg's user avatar
  • 1.1m
3 votes

Generate TikZ \pics dynamically

If we are using OpTeX, we don't need to use TikZ, because there is \puttext macro. The powerSupply.png image is saved to the \pwbox and this box is copied (and rotated) in the \fornum loop. \newcount\...
wipet's user avatar
  • 73.3k
7 votes

Get third argument from string?

mimic the definition of \@car but taking the third slot (this definition does not check the list is long enough) \documentclass[a4paper,12pt,dvipsnames*]{article} \makeatletter \newcommand{\firstof}[...
David Carlisle's user avatar
3 votes

How to create an alphanumerical Hash string with fixed length?

I show what to do with OpTeX. We have no already prepared macro like \_int_to_Base, so we have to use LauTeX primitives \Uchar, \uniformdeviate and we generate six digits in the loop. The probability ...
wipet's user avatar
  • 73.3k
2 votes
Accepted

Generate TikZ \pics dynamically

You can make use of the pgfplotstable package (which loads pgfplots and tikz in the background) and read in a CSV file which you can then process and use to place \pics or whatever you want to do. I'd ...
Jasper Habicht's user avatar
6 votes
Accepted

How to create an alphanumerical Hash string with fixed length?

If you don't use pseudorandom numbers elsewhere, \documentclass{article} \ExplSyntaxOn \sys_gset_rand_seed:n { 0 }% or whatever \NewExpandableDocumentCommand{\hash}{} { \int_to_Base:nn { \...
egreg's user avatar
  • 1.1m
0 votes
Accepted

\section, \subsection-derived commands end up twice in TOC, along with some floating 1s

\documentclass{article} \newcounter{hoofdstuk} % Define hoofdstuk counter independently \newcounter{afdeling}[hoofdstuk] % Define afdeling counter within the hoofdstuk counter \newcounter{...
BlueIris's user avatar
  • 339
1 vote

Problems testing for a leap year using expl3

Just for comparison. What we can do with classical plain TeX (without Lua): \newcount\modresult \newif\ifleap \def\modulo #1#2 {\modresult=#1\relax \divide\modresult by#2 \multiply\modresult by-#...
wipet's user avatar
  • 73.3k
1 vote
Accepted

Insert caption depending on condition

If a caption may or may not be present, it's an optional argument. Here I also provide for additional setting in \includegraphics and for the optional short caption for the list of figures. Beware ...
egreg's user avatar
  • 1.1m
0 votes

Insert caption depending on condition

This is the solution that I found, it is not the most elegant solution, but it works for me: \usepackage{ifthen} \usepackage{caption} \newcommand*\tkzimage[3]{ \begin{figure}[h!] \centering \begin{...
Andrumen1's user avatar
3 votes
Accepted

Parsing the Main Branch of Single-Branched SGF Files (with Regex?)

I think this takes care of all of the issues required by the OP. The challenge with this answer, compared with the OP's prior questions, is that a new valid syntax was introduced in the OP's ...
Steven B. Segletes's user avatar
3 votes

Place explicit character tokens of category 1/2, whose character codes are to be specified, before/behind tokens that form arbitrary macro argument?

When David Carlisle pointed out the right direction I realized that one can do with (256·2)+3 helper macros instead of 2562: \newcount\scratch \long\def\gobble#1{}% \chardef\nomoreromannumeral=0 % \...
Cattleya's user avatar
  • 419
3 votes
Accepted

Place explicit character tokens of category 1/2, whose character codes are to be specified, before/behind tokens that form arbitrary macro argument?

There is no reason to do this but if you define 256*256 helper functions you could do \def\macro#1#2{% \csname x\number#1x\number#2\endcsname } % then lots of definitions like {\catcode65=1\...
David Carlisle's user avatar
4 votes
Accepted

Can a command be used to build a row in bNiceMatrix?

The version 6.27 (2024-02-13) of nicematrix has a new key light-syntax-expanded designed to address that situation. When that key is in force, the body of the environment is expanded (in the TeX sens: ...
F. Pantigny's user avatar
  • 39.4k
1 vote

My defined macro doesn't work

If you want minimize/obfuscate the code, do it completely: \documentclass{article} \usepackage{tabularx} \newcommand\tx[3]{\begin{tabularx}{#1}{#2}#3\end{tabularx}} \begin{document} \tx{100pt}{|X|X|X|...
Fran's user avatar
  • 80k
5 votes

Replace each letter of a given word with blank boxes

Basically a one-liner with expl3: \documentclass{article} \providecommand{\textsquare}{% from amsthm \begingroup \usefont{U}{msa}{m}{n}\symbol{3}\endgroup } \ExplSyntaxOn \NewDocumentCommand{\...
egreg's user avatar
  • 1.1m
5 votes

Replace each letter of a given word with blank boxes

For good measure, a LuaLaTeX-based solution. Note that the argument of \mycommand can contain general utf8-encoded characters, not just ASCII-encoded characters; thus, \mycommand{你好世界} is entirely ...
Mico's user avatar
  • 502k
7 votes
Accepted

Replace each letter of a given word with blank boxes

\documentclass{article} \usepackage[T1]{fontenc} \usepackage{tokcycle,amssymb} \Characterdirective{\addcytoks{$\square$}} \newcommand\mycommand[1]{\tokencyclexpress#1\endtokencyclexpress} \begin{...
Steven B. Segletes's user avatar
0 votes

Double hashes inside macro definition?

An etl based approach. This normalises category code 1 and 2 tokens to braces ({}), otherwise should be meeting the criteria. \documentclass{article} \usepackage{etl} \ExplSyntaxOn \...
Skillmon's user avatar
  • 59.3k
1 vote

How to double explicit character tokens of category 6/parameter of an arbitrary sequence of tokens? (Not the \edef..{\unexpanded{..}}-thingie)

There are functions in etl that can search&replace macro parameter tokens. It has a few other shortcomings though (see its documentation, mostly it's about a few internal tokens not being allowed, ...
Skillmon's user avatar
  • 59.3k
0 votes

How to double explicit character tokens of category 6/parameter of an arbitrary sequence of tokens? (Not the \edef..{\unexpanded{..}}-thingie)

I am not sure if this token cycle meets your requirements: \documentclass{article} \usepackage[T1]{fontenc} \usepackage{tokcycle,lmodern} \tokcycleenvironment\showcats {\ifcatSIX\addcytoks{\thistok{##...
Steven B. Segletes's user avatar
2 votes
Accepted

Controlling Label Text in TikZ Nodes with \NewDocumentCommand: Including or Excluding Descriptions

You could set a optional token like this: \documentclass{article} \usepackage{tikz} \NewDocumentCommand{\myincludegraphics}{s O{0,0} O{1,1} t\displaytitle m}{% \IfBooleanTF{#1}{% \...
Tom's user avatar
  • 6,763
1 vote
Accepted

Controlling Coordinates of Rectangle in Image Inclusion

This can be easily achieved by xparse. Define star version of \myincludegraphics as default includegraphics without rectangle drawing. Then the non-star version will have two optional args to specify ...
Tom's user avatar
  • 6,763
2 votes
Accepted

Defining a Set of Keys in Terms of Another Set of Keys

(Edit: \a, \b and \c are already defined on the LaTeX 2ε-kernel. So let's use \MyA, \MyB and \MyC instead.) You can make keys defined for key-path /phili/testA but not defined for key-path /phili/...
Ulrich Diez's user avatar
  • 28.1k
1 vote

Can a command be used to build a row in bNiceMatrix?

While you can't use the & in the actual [light-syntax] environment, if you're willing to specify it in the command itself this worked for me: \usepackage{nicematrix} \newcommand{\MySecondRow}{a &...
abc123's user avatar
  • 23
0 votes

Numbers from command fed into a tikzpicture

For what I understand, this is not due to expandability, but to a misuse of \foreach. Just try to replace your \MV by its definition: it makes nonsense in line 9. It works flawless in line 7 since the ...
NBur's user avatar
  • 4,166
1 vote
Accepted

How to do an if after an `exp:args` in expl3

You can do: \ExplSyntaxOn % There is no point in doing \NewExpandableDocument as \clist_set:Nn and % \clist_pop:NN and \clist_map_inline:Nn are not fully expandable. \NewDocumentCommand{\...
Ulrich Diez's user avatar
  • 28.1k
1 vote
Accepted

No indent inside tcb theorem

Remove the key parbox=false. \documentclass{book} \usepackage[dvipsnames]{xcolor} \usepackage{geometry} \usepackage{amssymb} \usepackage[many]{tcolorbox} \newtcbtheorem[number within=section, list ...
mbert's user avatar
  • 4,086
2 votes

How to do an if after an `exp:args` in expl3

I wouldn't do it this way myself, but the following code works with relatively conservative changes to your original. \documentclass{standalone} \usepackage{tikz} % From [this answer by @...
cfr's user avatar
  • 196k
2 votes

Parsing a Flat SGF String (with the Package listofitems)

Here is a listofitems approach. However, it will not test for improper syntax in the SGF string, such as ;B[ab[;W]cd], which will respond in the same way as the test case \sgfA. \documentclass{...
Steven B. Segletes's user avatar
2 votes

How do you create a variadic new command?

There are tens of question on the site asking for “commands with a variable number of arguments”. You may do it, but it's best not to. A syntax such as \cmd[1,2,3] is much simpler. Here there's the ...
egreg's user avatar
  • 1.1m
2 votes

How do you create a variadic new command?

The following implements the argument structure as lain out in my comment, considering also the side note made by @Teepeemm. \documentclass{article} \ExplSyntaxOn \NewDocumentCommand \mycmd { O{} } ...
Skillmon's user avatar
  • 59.3k
3 votes

Passing Optional Arguments in a redefined command

Interestingly, the order in which the keywords are processed seems to matter, and width=\linewidth before rotate is not a good idea. OTOH, you can enter the width twice and the second one wins. \...
John Kormylo's user avatar
  • 79.2k
2 votes
Accepted

Parsing a Flat SGF String (with the Package listofitems)

I undestand that you are primarily interested in the parts of the string which have the syntax B[xy] or W[xy] (where x and y are some single letters). The below code therefore ignores everything else. ...
Jasper Habicht's user avatar
1 vote
Accepted

Loop Through Array of Strings (Actually an Array of Array of Strings, e.g. `{{black, ab}, {white, cd}}`)

I think, it is a better idea to process the nested clist (a comma-separated list) via expl tools here since tools already exist there to handle such lists: \documentclass{article} \usepackage{tikz} % ...
Jasper Habicht's user avatar
1 vote
Accepted

amsart problem with typsetting title

You forgot one that does work, namely \expandafter\title\expandafter{\the\mytitleA} Of course nothing avoiding \the in front of \mytitleA can work, because \the is necessary in order to deliver the ...
egreg's user avatar
  • 1.1m
5 votes

Inline If-Else Statement Based on Macro Argument

Much simpler: -black is white and conversely. \documentclass{article} \usepackage{tikz} % Parameters % % 1: dimension (in cm) % 2: board size (square) % % Example: A 19x19 board with size 10cm x ...
egreg's user avatar
  • 1.1m

Top 50 recent answers are included