31
votes
Accepted
Unbalanced dollar signs
First example:
\newcommand\weird{$$xyz$}
\weird$
\weird is expanded to $$xyz$ and processed;
The first $ is identified and TeX searches for a following $ since there's a difference in how $...$ and $...
24
votes
Ways to parse JSON in LaTeX?
Although the question is about parsing JSON in LaTeX, since the OP wants to "have my own style, which can be change very fast", I'll give a ConTeXt solution for its simplicity.
ConTeXt already comes ...
20
votes
What are tokens used for?
There are some technical differences but mostly it's just the "idiosyncratic" terminology used by D. Knuth, almost all systems are parsed in stages that separate lexical analysis (eg distinguishing a ...
19
votes
Accepted
Defining \tg so it behaves exactly like \sin and \cos?
All you need to do is write \DeclareMathOperator{\tg}{tg}; this defines the command \tg to use the normal math font. Then, you can define \tgx to just be \tg x:
\documentclass{article}
\usepackage{...
18
votes
Ways to parse JSON in LaTeX?
Try this:
\documentclass{scrbook}
\usepackage{luacode}
\begin{filecontents*}{test.json}
{
"recipe": {
"title":"First recipe",
"source":"My first cookbook",
"carbs":"1 oz",
"fat":"1 ...
17
votes
Ways to parse JSON in LaTeX?
As always, I missed David's comment, so my idea is not 100% original. :) As David said in the chatroom, he subliminally gave me a hint, which proves his powers are beyond comprehension, and that ducks ...
17
votes
Accepted
What is the difference between square brackets and curly brackets in a command invocation?
In TeX (and LaTeX and ConTeXt) braces ({}) are used as grouping characters.
In LaTeX, brackets ([]) are conventionally used for optional arguments, whereas braces are used for mandatory arguments.
...
16
votes
Dynamically count and return number of words in a section
Here is a pure LaTeX solution to the problem, using my tokcycle package (I even offer a pure TeX solution at the end!!). You will need the new version from 2021-03-10, because I define my environment ...
15
votes
Accepted
How does "\char`" work?
It is a misunderstanding to attach the left quote to the \char command as you do by writing \char`.
\char expects as argument a number. With the left quote you are converting the next character or ...
14
votes
Accepted
Parsing question: Do curly braces affect LaTeX compilation time?
The version without the braces is processed a tiny bit faster than the version with braces. Typesetting one million times $\frac12$ required 8.69 seconds, while a million $\frac{1}{2}$ took 8.81 ...
14
votes
Accessing TeX token or exporting filtered plain text
In ConTeXt, one option is to export your document as XML. You can then manipulate the resulting XML document using standard XML tools in your favourite programming language.
As an example, consider ...
14
votes
Accepted
Parser for pure LaTeX
If you write a parser you can define the subset of latex that you support. (There isn't really a useful definition of "Pure LaTeX with no primitives".)
For instance MathJax has a parser for a subset ...
14
votes
Unbalanced dollar signs
As explained in chapter 24 of the TeXbook, a (category code 3) $ cannot be in vertical mode: if it is found when TeX is in vertical mode, horizontal mode is started and $ is reexamined.
Chapter 25 ...
13
votes
How could LaTeX replace the tokens <= by the command \leq?
This is interesting problem which can be solved by more compact macros than in accepted answer:
\long\def\isnextchar#1#2#3{\begingroup\toks0={\endgroup#2}\toks1={\endgroup#3}%
\let\tmp=#1\...
13
votes
Accepted
When is \catcode executed?
TeX absorbs one record (typically a line) at a time, but doesn’t immediately tokenize it. It rather normalizes it changing the EOL character(s), if the operating system uses it (them), with the ...
12
votes
Accepted
Basics of parsing
The accepted TeX solution here includes several problems. One of them is that whole argument is read at each step, no only one token. The second is that the recursive loop in the accepted code ...
11
votes
How could LaTeX replace the tokens <= by the command \leq?
Here's a LuaLaTeX-based solution. It defines a Lua function, named do_subs, that performs substitutions on all the following two-letter and three-letter character combinations:
<== <=> ==>...
11
votes
Parser for decimal numbers in (plain) TeX
EDITED to seamlessly handle dots and/or commas as the decimal separator.
REEDITED to provide error checking. The error handler works by pre-processing the string and removing "numerically valid" ...
11
votes
How to go about creating custom parser in Latex
Version 1:
My answer is using mostly TeX primitives. I know there are other ways to do it, especially using expl3, but I'm not there yet, so...
I defined the main macro \makelinesegment that will ...
11
votes
Accepted
Why does LaTeX parse square brackets differently than curly braces?
TeX recognizes only two grouping characters. These are the characters assigned the catcodes 1 (begin group) and 2 (end group). By default they are { and } respectively. The [ and ] characters are ...
11
votes
Accepted
Automatic text highlighting based on a dictionary
Define dictionary words separated by || using the \setsepchar macro. Then, after \begin{document}, issue \def\currentword{}\tokencyclexpress and before \end{document}, issue \endtokencyclexpress. ...
10
votes
Accepted
Bash lstlistings treats "$#" as a comment
Whoever wrote the listings language for bash probably didn't anticipate this case. Actually, the listings package is a far cry from a proper lexical analyser and doesn't offer a clean way of doing ...
10
votes
Accepted
Parse argument with a number and an optional letter
\documentclass{article}
\makeatletter
\def\myparse#1{\afterassignment\xmyparse\@tempcnta#1\relax}
\def\xmyparse#1\relax{\csname myparsexx#1\endcsname}
\def\myparsexxb{Beginning of \the\@tempcnta}
\...
10
votes
Basics of parsing
Scanning one token at a time requires at least distinguishing whether the scanned token is a space or a left brace. This is because you can't remove the scanned token with a one parameter macro in ...
10
votes
Accepted
What are tokens used for?
Let me ignore the eyes–mouth–stomach (and…) terminology mentioned in the question, which is truly unique's to Knuth's description of TeX, and focus specifically on the tokens that the question asks ...
10
votes
Accepted
LaTeX3 - Parsing an argument to apply specific macros
You can parse via regular expressions.
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\parseanduse}{m}
{
\projetmbc_parseanduse:n { #1 }
}
\NewDocumentCommand{\macroO}{m}
{
\...
9
votes
How could LaTeX replace the tokens <= by the command \leq?
ConTeXt has this funny asciimath module, which has unfortunately completely different rules (which are highly inconsistent), but it still fits the general task though.
Probably there is something ...
9
votes
Parser for decimal numbers in (plain) TeX
this version is not expandable but it doesn't need any terminator, it just gobbles up as far as the end of the number. It is restricted to integers that fit in a tex count register.
\newcount\ca
\...
9
votes
Accepted
Control symbol formed by escape character followed by return
There's this in plain.tex
\def\^^M{\ } % control <return> = control <space>
\def\^^I{\ } % same for <tab>
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
parsing × 186macros × 52
tex-core × 21
external-files × 11
xparse × 10
expl3 × 9
tikz-pgf × 8
expansion × 8
environments × 7
strings × 7
arguments × 7
luatex × 6
listings × 6
latex3 × 6
optional-arguments × 5
xstring × 5
math-mode × 4
formatting × 4
lua × 4
grouping × 4
comma-separated-list × 4
token-lists × 4
tables × 3
spacing × 3
graphics × 3