GeneralxString - Problem with extracting from string

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Linus0000
Posts: 4
Joined: Wed May 15, 2024 2:45 pm

xString - Problem with extracting from string

Post by Linus0000 »

Hey everyone,

My problem is, that I want give strings a number. This works well. The problem with that is, that I'm not able to read back the given numbers for each string. I can only read back the number of the first entry, not the last both even. The Problem seems to be the read out, because the full assignment is right.

What I want is the output:
A) 1
B) 2
C) 3

Here is a minimal example:
\documentclass{article}
\usepackage{xstring}

\newcommand{\stringToNumberTable}{One:1;Two:2;Three:3;}

\begin{document}
A) \StrBetween{\stringToNumberTable}{One:}{;}\\
B) \StrBetween{\stringToNumberTable}{Two:}{;}\\
C) \StrBetween{\stringToNumberTable}{Three:}{;}\\

\stringToNumberTabTable
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

xString - Problem with extracting from string

Post by rais »

You're looking for the wrong ; character for second and third: the first occurance of ";" is before the first string ("Two:" / "Three:"). That's why nothing is returned. You could try

Code: Select all

Code, edit and compile here:
\documentclass{article}
\usepackage{xstring}
\newcommand{\stringToNumberTable}{One:1;Two:2;Three:3;}
\begin{document}
A) \StrBetween{\stringToNumberTable}{One:}{;}\\
B) \StrBetween[1,2]{\stringToNumberTable}{Two:}{;}\\
C) \StrBetween[1,3]{\stringToNumberTable}{Three:}{;}\\
\stringToNumberTable
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
KR
Rainer
Linus0000
Posts: 4
Joined: Wed May 15, 2024 2:45 pm

xString - Problem with extracting from string

Post by Linus0000 »

First of all, thanks for that fast help. But my problem is then not fully solved. So when I understand correctly, I need to know the position inside of the String. But thats no information I have for the application I want to do.

My goal is to implement a new commmand, which every time I use it gives a number to that string (each time increased by 1 staring at 1 -> 1, 2, 3, 4, and so on). With another command I want to read out the String to know, which number that String has. If I would know the position, I would know the Number. Therefore I need to read the number out without knowing the position inside of that string (It will be probably longer thant only 3 pairs).

I hope you can understand, what I mean with that.
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

xString - Problem with extracting from string

Post by rais »

Yes, I think I do. What's keeping you from creating your own string-between? Could look like this:

Code: Select all

Code, edit and compile here:
\documentclass{article}
\usepackage{xstring}
\newcommand{\stringToNumberTable}{One:1;Two:2;Three:3;}
\newcommand*\mytmp{}
\newcommand*\mystrbetween[3]{%
\StrBehind{#1}{#2}[\mytmp]%
\typeout{+++ (\mytmp)}%
\StrBefore{\mytmp}{#3}%
}
\begin{document}
A) \mystrbetween{\stringToNumberTable}{One:}{;}\\
B) \mystrbetween{\stringToNumberTable}{Two:}{;}\\
C) \mystrbetween{\stringToNumberTable}{Three:}{;}\\
\stringToNumberTable
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
This \mystrbetween will discard anything from the search string up to the first search string, before it looks for the second search string. This way, there won't be any second-search-string-found-before-first.
E.g., in the second case, the \StrBehind command will discard everything up to and including "Two:", then divert the resulting string (2;Three:3;) into macro \mytmp, which then is passed on to \StrBefore as first argument---there, the first ";" is the first one following 2 ;)

KR
Rainer
Linus0000
Posts: 4
Joined: Wed May 15, 2024 2:45 pm

xString - Problem with extracting from string

Post by Linus0000 »

Hey, this has worked. But one last question to the format. The Problem I have is, that I do not want this empty spaces between e.g. "Tab1:" and the other text behind, or the output of \nr. I tried to fix this by compressing the code (which made the spaces smaller but did not erase completely, and it then is also hard to read :/).

The Code looks like this right now.

Code: Select all

Code, edit and compile here:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{xstring}
\usepackage{xcolor}
\newcommand{\stringToNumberTabTable}{}
\newcommand{\stringToNumberFigTable}{}
\newcounter{StringCounterTab}
\setcounter{StringCounterTab}{1}
\newcounter{StringCounterFig}
\setcounter{StringCounterFig}{1}
\newcommand*\mytmp{}
\newcommand*\myStrBetween[3]{%
\StrBehind{#1}{#2}[\mytmp]%
\typeout{+++ (\mytmp)}%
\StrBefore{\mytmp}{#3}%
}
\newcommand{\nr}[1]{
\StrLeft{#1}{5}[\firstFiveCharsOfFirstString]
\StrLeft{SITab}{5}[\firstFiveCharsOfSecondString]
\StrLeft{SIFig}{5}[\firstFiveCharsOfThirdString]
\IfStrEq{\firstFiveCharsOfFirstString}{\firstFiveCharsOfSecondString}{\IfSubStr{\stringToNumberTabTable}{#1}{S\myStrBetween{\stringToNumberTabTable}{#1:}{;}}{\colorbox{red}{Label for table not defined.}}}{\IfStrEq{\firstFiveCharsOfFirstString}{\firstFiveCharsOfThirdString}{\IfSubStr{\stringToNumberFigTable}{#1}{S\myStrBetween{\stringToNumberFigTable}{#1:}{;}}{\colorbox{red}{Label for figure not defined.}}}{\ref{#1}}}}
\newcommand{\nl}[1]{
\StrLeft{#1}{5}[\firstFiveCharsOfFirstString]
\StrLeft{SITab}{5}[\firstFiveCharsOfSecondString]
\StrLeft{SIFig}{5}[\firstFiveCharsOfThirdString]
\IfStrEq{\firstFiveCharsOfFirstString}{\firstFiveCharsOfSecondString}
{\IfSubStr{\stringToNumberTabTable}{#1}{\colorbox{red}{Label already defined!}}{\edef\stringToNumberTabTable{\stringToNumberTabTable#1:\arabic{StringCounterTab};}\stepcounter{StringCounterTab}\relax}}{\IfStrEq{\firstFiveCharsOfFirstString}{\firstFiveCharsOfThirdString}{\IfSubStr{\stringToNumberFigTable}{#1}{\colorbox{red}{Label already defined!}}{\edef\stringToNumberFigTable{\stringToNumberFigTable#1:\arabic{StringCounterFig};}\stepcounter{StringCounterFig}\relax}}{\label{#1}}}
}
%Minimalbeispiel
 
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

xString - Problem with extracting from string

Post by rais »

Yes, that's called a significant space.
A newline character from the input inserts a space in the output. It doesn't matter, if this newline character is part of the text body or part of a command definition.
There's an easy way to `hide' this newline character, to prevent a command from adding spaces where none should be: end each line in your command definitions with a comment character (%), then the newline character is commented out.
And if you look closely you'll notice that I did that in my definition of \mystrbetween already.;)

On the other hand, leading spaces (at the beginning of a line) are usually ignored, that's why you can use indentation for better readability of your code without fear of creating yet another space in the output.

KR
Rainer
Linus0000
Posts: 4
Joined: Wed May 15, 2024 2:45 pm

xString - Problem with extracting from string

Post by Linus0000 »

Ok, that was new for me. I was always confused, why there is a % in the end of the line.

Thanks for you support :)
Post Reply