Questions tagged [parsing]
{parsing} is the process of analysing a string of symbols, either in natural or computer language.
186
questions
102
votes
5
answers
13k
views
Is there a BNF grammar of the TeX language?
I'm looking for a BNF grammar of the TeX language, does it exist?
EDIT
For those of us who are not computer scientists, a BNF grammar is one kind of formal description of a CFG: Backus Naur Form.
...
43
votes
6
answers
17k
views
Dynamically count and return number of words in a section
I am writing a proposal for which it is required to give the number of words of the main section in the proposal itself. I found this answer on how I can get a word count on a LaTeX document:
Is ...
34
votes
7
answers
3k
views
How could LaTeX replace the tokens <= by the command \leq?
How could LaTeX replace the tokens <= by the command \leq efficiently?
Example 1: I have this code:
\[
2x <= 4x - 2
\]
And I want to get after the compilation this:
Example 2:
\[
q -...
27
votes
2
answers
22k
views
How to write uncertainties with the \pm symbol "10 ± 1 K" by using siunitx?
Using the siunitx package, if I specify an error with the \pm symbol then it automatically converts it to a form using parenthesis. I would like it to not do this and keep the \pm symbol, but still ...
27
votes
3
answers
1k
views
Unbalanced dollar signs
I'm curious: why do the following two examples behave differently?
Example 1: (Works, displays xyz in displaymath mode.)
\newcommand\weird{$$xyz$}
\weird$
Example 2: (Error, missing $ inserted.)
\...
25
votes
3
answers
909
views
What are the effects of an empty group, {}?
In order to have a syntactic space after a no-argument command, one is taught to delimit the command token with an empty group ({}), like this: \somecommand{} more LaTeX material.
However an empty ...
24
votes
2
answers
4k
views
What is the difference between square brackets and curly brackets in a command invocation?
Pardon the naive beginner question.
In a command like: \textcolor[HTML]{4C7045}{dark green text}, or \documentclass[12pt]{article}, or \begin{figure}[htb], why are some arguments surrounded by [] and ...
22
votes
5
answers
2k
views
Basics of parsing
I'm trying to master the art of parsing text one character at a time. The following is what I've discovered so far. Is this as good as it gets? I would really prefer not to do all those \edefs as ...
21
votes
4
answers
15k
views
Ways to parse JSON in LaTeX?
I'm writing my recipes (food) in a small textfile in JSON. Are there any ways to parse this in LaTeX?
I know, that are lots of templates dealing with recipes, but I want to have my own style, which ...
20
votes
5
answers
846
views
How to define a command that surronds an argument with parenthesis if it contains a comma?
I have a very simple command in which one of the arguments can contain commas, in which case the output may not be easily readable since the text in which it is used may contain other commas, making ...
20
votes
4
answers
10k
views
What parsers for (La)TeX mathematics exist outside of the TeX engines?
Inspired by the author's motivation for asking Is there a BNF grammar of tex language.
Are there any well done libraries that can parse some subset of TeX mathematics independently of the TeX engine? ...
19
votes
4
answers
4k
views
Syntax of TeX/LaTeX
{} and [] do some kind of grouping, and comma seems to be special as well. Being a programmer I find it frustrating to use Latex on incantation-only basis. I would like to know how does it exactly ...
18
votes
4
answers
5k
views
Bash lstlistings treats "$#" as a comment
Background: In Bash, $# represents the number of arguments, and # starts a comment.
Consider the following Bash script:
# This is a comment
while [[ $# -gt 0 ]]; do
echo $1
shift
done
Note ...
18
votes
1
answer
12k
views
How does 'filecontents' keep LaTeX parsing while temporarily stop writing output
We all know filecontents.sty can do
\documentclass{article}
\usepackage{filecontents}
\begin{document}
Hello World
\begin{filecontents}{dummy.txt}
No one will read this if I don't use it ...
16
votes
5
answers
660
views
Applying an operation only to capitals
I'd like to apply a function to capitals only so that:
\emphcaps{YaWeH}
Outputs like:
\emph{Y}a\emph{W}e\emph{H}
Preferably without using too many packages (I already use memoir).
NOTES: I use ...
15
votes
5
answers
2k
views
How do I write a macro having comma-separated and variable number of arguments?
I want to write a macro in LaTeX2e that can pass \textcolor, which is embedded in another macro, the values of the colour model gray, rgb, or cmyk. The number of arguments specified implies the colour ...
14
votes
4
answers
3k
views
Drawing Young tabloids
I would like LaTeX to produce output like the following
(without the 2 a so called Young tabloid), ideally by writing
\ytabloidshort{123,56}
(using one character per cell, ideally allowing groups ...
14
votes
2
answers
746
views
What are tokens used for?
When reading TeX for the impatient, we learn that:
The "eyes" of TeX convert the input file into a sequence of characters;
Then the "mouth" of TeX converts the character sequence into tokens;
Then ...
14
votes
2
answers
2k
views
Parsing files through LuaTeX
This question has two parts:
How would one generally parse a file with LuaTeX, assuming the file only holds data
that is delimited either by spaces, commas or semicolons.
How would you parse the ...
13
votes
5
answers
2k
views
Parsing hexadecimal numbers to binary and iterating over bits
I'm currently working on a software for an graphic display, so I recreated it in TikZ for documentation purposes:
I currently use this to set individual pixels on the display:
\setpixel{x}{y}
Where ...
13
votes
5
answers
2k
views
Parse comma-separated list of command names with spaces
Problem
This question is related to How can I specify a long list of math operators? – but with a twist: I want to allow spaces in in the comma-separated list, like so:
\newacronyms{acm, ams, cpu, ...
13
votes
1
answer
336
views
Parsing question: Do curly braces affect LaTeX compilation time?
I have an old and slow computer and long LaTeX files. When typing I prefer to use \frac 1{x} instead of \frac{1}{x} or x_i instead of x_{i}.
Does the presence or omission of curly braces in these ...
13
votes
1
answer
916
views
Parsing commands with multiple optional arguments
Some LaTeX commands accept multiple optional arguments. Here are some examples:
\makebox[<width>][<pos>]{<text>}
\framebox[<width>][<pos>]{<text>}
\parbox[<pos&...
13
votes
1
answer
175
views
What is considered a token for non-standard input encodings?
When I use an input encoding to (La)TeX that allows me to directly input characters such as é and ä (such as by declaring \usepackage[utf8]{inputenc}), how is a token defined? Specifically,
is it ...
12
votes
3
answers
461
views
Accessing TeX token or exporting filtered plain text
(updated according to gained information)
In the context of keyword extraction from arbitrary (La)TeX documents and simple manipulations with them (automated annotation), I would like to be able to ...
12
votes
1
answer
10k
views
pgfplot: Datafile format for datetime field
I have a datafile (which I may manipulate to any format), it contains a date-time stamp currently in the form "year-month-day hour:min" and a single variable for each recorded time.
When I attempt to ...
12
votes
2
answers
2k
views
Iterate over space-separated list
I’ll briefly explain the background in case this can be solved simpler but I’d still be interested in an answer to the actual question, too.
My chapter titles are very narrow and use a large font so ...
12
votes
1
answer
4k
views
is there a python module for parsing LaTeX?
I am looking to write python programs that modify LaTeX source files. To do this I would like to have a basic parser in python that can reliably read and write LaTeX files while maintaining the tree. ...
11
votes
6
answers
784
views
How to go about creating custom parser in Latex
Say I wanted to write a macro like this:
\makelinesegment{1:1-1:2-2:2}
And have it become something like:
\draw (1,1) -- (1,2) -- (2,2);
The question is, how to go about parsing that 1:1-1:2-2:2 ...
11
votes
3
answers
11k
views
Defining \tg so it behaves exactly like \sin and \cos?
In my country, instead of tan, we use tg (this is just one of them). I want to be able to type
\tg{x}
and possibly
\tgx
So that Latex recognizes them. I tried defining them like this:
\...
11
votes
3
answers
624
views
Parser for decimal numbers in (plain) TeX
I need help writing a parser for decimal numbers using only TeX and plain TeX. The set of accepted inputs is given by the regular expression:
(+-)?(0-9)*(.,)?(0-9)+
The parser need not do anything ...
11
votes
2
answers
1k
views
The different meanings of LaTeX's \stop command
we know that
\stop
will cause LaTeX to stop parsing and output what it has processed up to this command.
But then I discovered another way to use it, as demonstrated in this minimum working example:...
11
votes
2
answers
577
views
When is \catcode executed?
According to the TeXbook, characters in a file are first converted to tokens with catcodes ("mouth") and then any nonexpandable commands are executed ("stomach"). \catcode is a nonexpandable command, ...
11
votes
1
answer
6k
views
How does "\char`" work?
In LaTeX, you can use \char` followed by a symbol to get that symbol. For example \char`b or \char`\b would just yield the letter b.
Which symbols/letters does that work for? Two ...
11
votes
1
answer
3k
views
Automatic dropcaps for the first letter of a chapter
I'm in the process of writing a LaTeX style for thesis dissertations, and would like to automatically transform the first word of a chapter to dropcaps. This is trivial to do manually using the ...
10
votes
5
answers
596
views
Parse dotted string, extracting last part
I want a macro that will separate a string like "1.2.3" into two parts: the part until the last dot (call that the context) and the last number. This seems a pretty basic application of a helper ...
10
votes
4
answers
2k
views
Only get filename and extension of listing, not whole path
I am trying to add the file name as caption. I was able to do this setting
caption=\lstname
but this shows the full path. I am now trying to get the name of the file and extension to show only. I ...
10
votes
2
answers
920
views
\def taking next word as argument?
This question is a variant of my previous question requesting a macro that takes the rest of the line as argument. I would like to write a macro that would take the next word as argument, where word ...
10
votes
1
answer
126
views
Scan argument for parentheses
I'd like to define a macro which expands to (#1) if #1 does not contain any parentheses, but expands to \big(#1\big) if it does.
I'd choose simplicity over generality: if a solution works in the most ...
10
votes
1
answer
271
views
How do I define a new TeX register with arguments in LuaTeX?
Some time ago I tried to implement the \pdfadjustinterwordglue from pdfTeX in LuaTeX using a callback. Getting the callback to work is pretty simple, but there is another major problem in the way ...
10
votes
1
answer
820
views
Extract the logically first slide in a Beamer overlay specification?
When writing Beamer slides for a talk, I find that I often want to have some text (usually math) visible on a group of slides and be alerted on only the first slide in the group. For example, I can ...
9
votes
4
answers
472
views
LaTeX3 - Parsing an argument to apply specific macros
In the following code, I would like to parse the argument of \parseanduse such as to "achieve" the examples shown.
Each time a o is met, the following integer should be used to call the ...
9
votes
3
answers
527
views
How to parse multiple arguments in TeX macros
I have dates formatted {yyyy-mm-dd}. I would like display the time elapsed between the dates in the form Y years, M months. Given the format of the dates, it seems easier to use arithmetic than to ...
9
votes
3
answers
1k
views
Implementing car and cdr in TeX
I've been trying to implement a macro that reads and transforms its argument into a different form...but I'm having quite a bit of trouble finding or creating even the most basic parsing tools.
Case ...
9
votes
3
answers
157
views
Parsing various types of page ranges with a single command
Having to treat a considerable amount of page ranges, I'd like to have a robust and simple command that would parse and print them correctly.
The most simple case is e.g.
264,15-26
which means:
...
9
votes
1
answer
586
views
Replacing Leading Minus Signs in Environment
In this question concerning negative signs and the alignment of entries in matrix environments, Heiko Oberdiek gave a great answer in which he uses a user-defined command called \matminus instead of ...
8
votes
4
answers
505
views
Read command with varying input arguments
Out of curiosity I'd like to define a LaTeX command that looks like:
\newcommand{\trigger}{out1,out2,...,outn}
with a rather strange function (at least, I couldn't find anything like this elsewhere)...
8
votes
4
answers
485
views
Check for group as argument (expandable)
I want to check whether an argument of a command is fully contained in a pair of curly braces without using any extra packages. For this I define two macros containing { and } with catcode 12, ...
8
votes
4
answers
573
views
Runaway argument? with \newcommand[2]
I have a date (in the format YYYY.MM.DD) and a time (format HH:MM).
From these, I want to compute a kind of TimeStamp/Total of minutes.
\documentclass{article}
\makeatletter
% Timestamp
\...
8
votes
2
answers
1k
views
Parse command argument separated by comma
I want to mix several language in my document, and the output language can be chosen according to corresponding variable
I want to define this kind of command
\mulan[en,fr,ru]{arg1}{arg2}{arg3}
to ...