Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.
16 votes

How to split input string in a LaTeX command?

This is very easy with xparse: \documentclass{article} \usepackage{xparse} \NewDocumentCommand{\myfunc}{ >{\SplitList{,}} m }{% \ProcessList{#1}{\func}% } \NewDocumentCommand{\func}{m}{% \...
user avatar
  • 986k
15 votes

Sorting a comma-separated list with LaTeX?

How about Lua? \def\sortlist#1{% \directlua{% local t = { #1 } table.sort(t) tex.sprint(table.concat(t,", ")) }% } \sortlist{"World", "Hello"} \bye Addendum: One of the really ...
user avatar
13 votes

How to split input string in a LaTeX command?

LaTeX has a built in command for iterating over comma separated lists: \documentclass{article} \makeatletter \newcommand\myfunc[1]{\@for\tmp:=#1\do{\func{\tmp}}} \let\func\fbox \begin{document} \...
user avatar
13 votes
Accepted

Sorting a comma-separated list with LaTeX?

This seems to work, with no packages. EDITED to solve the upper/lower-case problem. EDIT: Resolved problem when a comparison ran out of letters prior to resolving the order, for example, wash, ...
user avatar
12 votes
Accepted

Combine `map` and `use` expl3 macros

You can use two sequences; first apply the mapping to get the “adorned” items, then use them. \documentclass{article} \usepackage{expl3} \usepackage{xparse} \ExplSyntaxOn \seq_new:N \...
user avatar
  • 986k
11 votes

Command with arguments separated by comma II (typically an easy way to write matrices)

The argument is first split at commas in a sequence which is mapped taking into account the positions, so we can add & or \\ as needed. \documentclass{article} \usepackage{amsmath} \ExplSyntaxOn \...
user avatar
  • 986k
10 votes

Sorting a comma-separated list with LaTeX?

You can safely compare strings consisting of ASCII characters; words with accented characters will not work. \documentclass{article} \usepackage{expl3,l3sort,xparse} \ExplSyntaxOn \...
user avatar
  • 986k
9 votes

How to check if comma list is empty?

The test you want is \clist_if_empty:N(TF): each data type has an appropriate test for emptiness or similar. These are defined such that they are 'aware' of the internal nature of data types (for ...
user avatar
  • 243k
9 votes

How can I create a comma-separated list of characters from a text string?

Here's a LuaLaTeX-based solution. It's fully utf8-code aware, i.e., the input string may contain utf8-encoded, as opposed to just ascii-encoded, characters. The code consists of a Lua function named ...
user avatar
  • 435k
8 votes
Accepted

Making \cpageref output commas instead of "and"

If you need this behavior all over the document, simply add the following line in your preamble \newcommand{\creflastconjunction}{,\nobreakspace} MWE: \documentclass{article} \usepackage[sort]{...
user avatar
8 votes

How to iterate over a comma separated list?

listofitems provides a pgffor-like interface for iterating over a list: \documentclass{article} \usepackage{listofitems} \begin{document} \setsepchar{,}% Parse-character = ',' \readlist\mylist{...
8 votes
Accepted

Coding a *short* newcommand with a variable number of arguments for a very specific use-case

Does the following solution meet your expectations for simplicity and flexibility? :-) Instead of thinking of each row of each of the two column vectors being a separate argument, you may want to ...
user avatar
  • 435k
8 votes

Building and using a list

This is essentially in expl3 out of the box. The idea is to map the original list; if an item is not in the remove list, add it to a temporary list and, at the end, reset the original list to the ...
user avatar
  • 986k
8 votes
Accepted

How to get the index variable in the foreach loop

Use count=\n: \documentclass{article} \usepackage{pgf, tikz} \begin{document} \textbf{Comprehensive knowledge storage follows the evolutionary sequence given by} \foreach \x in {letter, word, phrase, ...
user avatar
  • 986k
8 votes
Accepted

Command with arguments separated by comma II (typically an easy way to write matrices)

I am highly unused to the LaTeX3 syntax too, so I give you non-exp3 solution based only on TeX primitives. You need not any LaTeX package (and you basically don't even need LaTeX): \documentclass[12pt]...
user avatar
  • 52.5k
7 votes
Accepted

Beamer and a for each loop

One issue you currently have is trying to create a \frame inside another frame. Moreover, \frame takes an argument, which you're not supplying in a proper way. Here is the etoolbox implementation ...
user avatar
  • 565k
7 votes

expl3: How to remove the final comma in a clist mapping

Without using sequences, you can pop the leftmost item in the clist and map the remaining clist: \documentclass{article} \usepackage{expl3} \ExplSyntaxOn \tl_new:N \l__UF_clist_use_head_tl \...
user avatar
  • 986k
7 votes
Accepted

Escaping special characters for use as a URL

\documentclass{article} \usepackage{url} \usepackage{pgffor} \usepackage{xparse} \usepackage{xstring} \usepackage[colorlinks=true]{hyperref} \NewDocumentCommand{\FormatLinks}{% s% #1 =* not ...
user avatar
7 votes
Accepted

Handling _ (underscores) in a macro's comma-separated list of arguments

You can use \detokenize; but if you want a printed underscore, you need to load fontenc with the T1 option. \documentclass{article} \usepackage[T1]{fontenc} \usepackage{etoolbox} \usepackage{hyperref}...
user avatar
  • 986k
7 votes
Accepted

Commas in \clist_map_inline:nn split values in undesired places

Any comma in the input defines an item: there's no way to distinguish a “grammatical comma” from a “separator comma” if they're at the same brace level. Besides bracing each item, which might be ...
user avatar
  • 986k
7 votes

Command with arguments separated by comma II (typically an easy way to write matrices)

Here I use tokcycle to replace the commas with & separators. WHile I show 2x2 and 3x3, any number of columns are acceptible, as long as the matrices contain 2 or (optionally) 3 rows. \...
user avatar
7 votes

How can I create a comma-separated list of characters from a text string?

Here I use a token cycle. No mention was made of macros or groups appearing in the input (only characters and spaces), and so those should be excluded from the input, unless the OP advises on how ...
user avatar
7 votes
Accepted

Create macro with variable number of comma separated arguments in plain TeX?

The solution in very simple in Plain TeX (or more precisely: using only TeX primitive commands) % fixed number of args: \def\frc#1{\frcA#1\end} \def\frcA#1,#2\end{{#1\over #2}} test: $\frc{a,b}$ % ...
user avatar
  • 52.5k
6 votes
Accepted

Long List of Files Names with Paths

It is generally more efficient to arrange that the list is executable rather than use a comma separated list (which needs to be parsed), traditionally LaTeX uses \@elt for this but any macro will do, ...
user avatar
6 votes

Iterator-like command, where each use expands to an item from list defined earlier

It's a breeze with expl3: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\deflist}{O{,}m} { \seq_set_split:Nnn \l_narebski_list_seq { #1 } { #2 } } \...
user avatar
  • 986k
6 votes
Accepted

Select largest value from a list of variables in LaTeX

Use the max function provided by expl3 in the fp module: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\findLargest}{ om } { \IfNoValueTF{#1} {% separator is a ...
user avatar
  • 986k
6 votes

Membership check on comma separated list

LaTeX has such a test built in already, so you just need to define your requested syntax: \documentclass{article} \makeatletter \newcommand\ifmember[2]{% \in@{#1}{#2}% \ifin@ \expandafter\@...
user avatar
6 votes
Accepted

newcommand with comma separated argument, and optional arguments

The following example uses \comma@parse of package kvsetkeys to parse the comma separated argument lists. \documentclass{report} \usepackage{kvsetkeys}% provides \comma@parse \usepackage{etexcmds}% ...
user avatar
6 votes

Handling _ (underscores) in a macro's comma-separated list of arguments

The simplest way to do this is to load the url package and define a url command to format the refnames: \usepackage{url} \DeclareUrlCommand{\coderefname}{\urlstyle{rm}} \newcommand{\codecitep}[1]{% ...
user avatar
  • 201k
6 votes
Accepted

How to create list and assign it to variable?

You have to “normalize” the list: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\multiinclude}{ mO{} } { \clist_set:Nx \l_sergio_mi_clist { #1 } \...
user avatar
  • 986k

Only top scored, non community-wiki answers of a minimum length are eligible