19 votes
Accepted

Extract first word in a string

You're almost there, just remove the trailing comma \documentclass{article} \makeatletter \newcommand\FirstWord[1]{\@firstword#1 \@nil}% \newcommand\@firstword{}% \newcommand\@removecomma{}% \def\@...
egreg's user avatar
  • 1.1m
15 votes

Base64 encoding in LaTeX?

Interesting programming task! :) Here's a base64 encoding and decoding code. To encode, it uses \int_to_bin:n on each character, padding to 8 bits, then separates the output in chunks of 6 bits and ...
Phelype Oleinik's user avatar
14 votes
Accepted

Looping over strings

If I modify your test a bit to make a shorter argument for tracing \documentclass{article} \def\test#1{{ \tracingonline=1 \tracingmacros=1 \markletters{#1} } \typeout{TYPEOUT: \markletters{#1}} } \...
David Carlisle's user avatar
14 votes
Accepted

Which engines can compare strings expandably?

In addition to pdfTeX itself, e-pTeX and e-upTeX provide the \pdfstrcmp primitive under that name. In XeTeX, the same idea is implemented as \strcmp: the lack of pdf reflects the fact this is not PDF-...
Joseph Wright's user avatar
  • 260k
14 votes

Color coding in Python f-strings

I couldn't find an answer using the listings package. However, I reckon the minted package understands Python f-strings: \documentclass{article} \usepackage{minted} \begin{document} \begin{minted}{...
NVaughan's user avatar
  • 8,185
13 votes
Accepted

Test if an argument starts with a particular string

If your first word is always separated from the rest by a space, you can do with the following code: \documentclass{article} \makeatletter \newcommand{\transform}[1]{\transform@#1\@nil} \def\...
egreg's user avatar
  • 1.1m
12 votes

Most efficient way to generate every possible capitalization of a string?

Note quite idiomatic L3 but \documentclass{article} \def\zz#1{\xzz{}#1\relax} \def\xzz#1#2#3\relax{% \ifx\relax#3\relax \zzdo{#1#2}% \zzdo{#1\uppercase{#2}}% \else \xzz{#1#2}#3\relax \xzz{#1\...
David Carlisle's user avatar
12 votes

Incorrect value of Pi in Lua after 16th Decimal Place

You can cheat and do this using Metapost via the luamplib package. \documentclass{article} \usepackage{luamplib} \mplibtextextlabel{enable} \mplibnumbersystem{decimal} \begin{document} This is $\pi$: ...
Thruston's user avatar
  • 42.3k
11 votes
Accepted

Linebreaks in long character strings

There is already a package for this: \documentclass{article} \usepackage{seqsplit} \newcommand{\hash}[1]{{\ttfamily\seqsplit{#1}}} \begin{document} SHA-256 is a hash function with a 256-bit long ...
egreg's user avatar
  • 1.1m
11 votes

How can I check if the value of a variable is the same as what another variable?

Well your question is not simple. At first you didn't explain what you mean by "same for you" so the rules for your "fuzzy" comparision are quite unclear. And at second you didn't explain what content ...
Ulrike Fischer's user avatar
11 votes

Convert numbers to words, e.g., 111 = one hundred eleven, using the "Indian numbering system"

Here's an easy version spelling out currencies (as an example with Euros). Please note that you have to input the number and its decimal places separately. \documentclass{article} \usepackage{numname}...
TeXnician's user avatar
  • 33.6k
11 votes
Accepted

How to define a comma separated list of arguments for includegraphics

Just define new keys: \documentclass{article} \usepackage{graphicx} \makeatletter \define@key{Gin}{settingsA}[]{\setkeys{Gin}{width=\textwidth, angle=90}} \define@key{Gin}{settingsB}[]{\setkeys{Gin}{...
egreg's user avatar
  • 1.1m
11 votes
Accepted

Incorrect value of Pi in Lua after 16th Decimal Place

From the Lua manual, section 2.1 Standard Lua uses 64-bit integers and double-precision (64-bit) floats, but you can also compile Lua so that it uses 32-bit integers and/or single-precision (32-bit)...
egreg's user avatar
  • 1.1m
11 votes

Simple implementation of the abs function by getting rid of or by consuming the "-"?

You could use xfp (the package is very short, it contains only two definitions to create wrappers around \fp_eval:n and \int_eval:n): \documentclass{article} \usepackage{xfp} \begin{document} \fpeval{...
Ulrike Fischer's user avatar
11 votes
Accepted

Base64 encoding in LaTeX?

LuaTeX ships with LuaSocket whose MIME module supports a bunch of common encodings and decodings. All codeblocks are one file: \documentclass{article} \usepackage{luacode} \begin{luacode*} local mime ...
Henri Menke's user avatar
11 votes

xstring - How to define a macro that removes final punctuation?

Easy with expl3: \documentclass{article} %\usepackage{xparse} % uncomment for LaTeX prior to 2020-10-01 \ExplSyntaxOn \NewDocumentCommand{\removePunct}{m} { \tl_set:Nn \l_tmpa_tl { #1 } \...
egreg's user avatar
  • 1.1m
10 votes

How insert a character at the begin of every line from a source code?

With listings you can redefine the numberstyle: \documentclass{article} \usepackage{listings} \lstset{ language=tex, basicstyle=\footnotesize\ttfamily\selectfont, keepspaces=true, numbers=...
Ulrike Fischer's user avatar
10 votes
Accepted

Splitting Strings by Comma

listofitems is a powerful list parsing package. Your example is the simplest of its capability: \documentclass{article} \usepackage{listofitems} \begin{document} \def\pleaseHelp{I, am, confused} \...
Steven B. Segletes's user avatar
10 votes

Convert numbers to words, e.g., 111 = one hundred eleven, using the "Indian numbering system"

(updated the answer to allow for fractional parts in the numbers as well as for very large numbers (>10^12)) (Sept. 2019: Updated 1 line in the Lua code, to make it work correctly under versions of ...
Mico's user avatar
  • 508k
10 votes
Accepted

Smart-expansion of a range to a list of numbers

With a little bit of code you can make yourself a parser. I defined \makeProblems{<integer list>}{<code>} for you, in which <integer list> is a comma separated list of numbers where &...
Phelype Oleinik's user avatar
10 votes
Accepted

Checking if an integer is a member of an integer list

You can use do a comma-separated list parser to loop through the list of items and check if the given item is present in the list. Since the comma-separated list can be read by using a delimited macro,...
Phelype Oleinik's user avatar
10 votes
Accepted

Expandable case insensitive switch case for string comparison

With the kind help of expl3, this is expandable (if the code of the cases is, so with the normal definition of \selectlanguage it isn't). EDIT: as pointed out by @JosephWright, \str_foldcase:e is to ...
Skillmon's user avatar
  • 60.8k
10 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 ...
Mico's user avatar
  • 508k
10 votes
Accepted

Reformat a string and remove leading zeros from substrings

\documentclass{article} \def\foo#1{\xfoo#1\relax} \def\xfoo#1--#2-#3\relax{#1, p.\ \number#2, fig.\ \number#3\relax} \begin{document} \foo{2011-10-29--2-03} \end{document}
David Carlisle's user avatar
9 votes
Accepted

Unexpanded string replace

By default xstring doesn't look into groups. But you can force it to consider also the text in braces: \documentclass{article} \usepackage{xstring} \begin{document} \noexpandarg \StrSubstitute{John {...
Ulrike Fischer's user avatar
9 votes

strip last character from parameter if it is 's'

Here is an example how to strip the last s using TeX primitives. The expandable macro \striplastS is created. \striplastS{texts} expands to text and \striplastS{text} expands to text too. \def\...
wipet's user avatar
  • 74.7k
9 votes
Accepted

LaTeX count number of occurrences of a character in a string

Using expl3 you can split the token list into items and then count the items (less 1): 2 4 \documentclass{article} \usepackage{xparse} \ExplSyntaxOn % \CountSubStr{<substring>}{<...
Werner's user avatar
  • 604k
9 votes

Looping over strings

There are two differences between the first solution and the other two: The second and third solution both use tail recursion, in contrast to the first one. And, related to this, the first solution ...
gernot's user avatar
  • 49.6k
9 votes

Looping over strings

The solutions by Tarass and David Carlisle are not that different. The latter is more efficient, because it removes the conditional before going to the next cycle, whereas the former nests them and, ...
egreg's user avatar
  • 1.1m

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