LaTeX forum ⇒ GeneralxString - Problem with extracting from string

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

xString - Problem with extracting from string

Postby Linus0000 » Wed May 15, 2024 4:24 pm

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 2021:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics
rais
Posts: 412
Joined: Sun Nov 16, 2014 8:51 pm

xString - Problem with extracting from string

Postby rais » Wed May 15, 2024 9:33 pm

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
\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}

KR
Rainer

Linus0000
Posts: 3
Joined: Wed May 15, 2024 2:45 pm

xString - Problem with extracting from string

Postby Linus0000 » Thu May 16, 2024 9:44 am

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: 412
Joined: Sun Nov 16, 2014 8:51 pm

xString - Problem with extracting from string

Postby rais » Thu May 16, 2024 7:18 pm

Yes, I think I do. What's keeping you from creating your own string-between? Could look like this:
\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}

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: 3
Joined: Wed May 15, 2024 2:45 pm

xString - Problem with extracting from string

Postby Linus0000 » Tue May 21, 2024 3:14 pm

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.
\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

\begin{document}
	\section{nl}
	Tab1:\nl{SITab1}gibt nichts aus\\
	Tab2:\nl{SITab2}gibt nichts aus\\
	Tab3:\nl{SITab3}gibt nichts aus\\
	Fig1:\nl{SIFig1}gibt nichts aus\\
	Fig2:\nl{SIFig2}gibt nichts aus\\
	Fig3:\nl{SIFig3}gibt nichts aus
	
	\section{nr}
	Tab1: \nr{SITab1} gibt was aus\\
	Tab2: \nr{SITab2} gibt was aus\\
	Tab3: \nr{SITab3} gibt was aus\\
	Fig1: \nr{SIFig1} gibt was aus\\
	Fig2: \nr{SIFig2} gibt was aus\\
	Fig3: \nr{SIFig3} gibt was aus
	
	\section{Stings}
	A) \stringToNumberTabTable\\
	B) \stringToNumberFigTable
\end{document}

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

xString - Problem with extracting from string

Postby rais » Tue May 21, 2024 7:49 pm

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


Return to “General”

Who is online

Users browsing this forum: Baidu [Spider] and 14 guests