Text FormattingConvert string to integer

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
SAKDOSS
Posts: 14
Joined: Thu Feb 21, 2013 5:06 pm

Convert string to integer

Post by SAKDOSS »

Hello,

I want to make a function which take 4 arguments (#1 to #4) and return the size of :
- the size of the longest string between #1 and #2
plus
- the size of the longest string between #3 and #4

For example if I do:

Code: Select all

\myFunction{a}{bc}{def}{g}
it should return 5 (because max(length(a, bc)) = 2, max(length(def, g)) = 3 and I add them).

I found how to make a function to find the max between to arguments:

Code: Select all

\usepackage{ifthen}
\usepackage{xstring}

\newcommand{\maxL}[2]{
  \StrLen{#1}[\lun]
  \StrLen{#2}[\ldeux]
  \ifthenelse{\lun > \ldeux}{\lun}{\ldeux}
}
To get the two expected values I just have to do:

Code: Select all

\maxL{#1}{#2}
\maxL{#3}{#4}
My problem occurs when I want to sum this two value. It seems that they are string and not integer. So if I do \maxL{a}{bc} + \maxL{def}{g} it will return "2 + 3" instead of the expected 5.

I tried without success to use \setcounter to convert the values. The following code give me "ERROR: Missing number, treated as zero":

Code: Select all

\newcounter{result}
\newcommand{\test}[2]{\setcounter{result}{\maxL{#1}{#2}}}
...
\test{a}{bc}
Do you know how I can do this ?
Last edited by cgnieder on Sat Feb 23, 2013 5:25 pm, edited 2 times in total.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

SAKDOSS
Posts: 14
Joined: Thu Feb 21, 2013 5:06 pm

Convert string to integer

Post by SAKDOSS »

I found a workaround:

Code: Select all

\newcounter{result}

\newcommand{\myFunction}[4]{

  \setcounter{result}{0}
  \StrLen{#1}[\sOne]
  \StrLen{#2}[\sTwo]
  \ifthenelse{\sOne>\sTwo}{\addtocounter{result}{\sOne}}{\addtocounter{result}{\sTwo}}

  \StrLen{#3}[\sThree]
  \StrLen{#4}[\sFour]
  \ifthenelse{\sThree>\sFour}{\addtocounter{result}{\sThree}}{\addtocounter{result}{\sFour}}

  \arabic{result}

}
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Convert string to integer

Post by cgnieder »

You might like »expl3«, the LaTeX3 programming layer. With expl3 the task is very straight forward:

Code: Select all

\documentclass{article}

\usepackage{expl3,xparse}

\ExplSyntaxOn
\int_new:N \l__my_tmpa_int

\cs_new:Npn \my_function:nnnn #1#2#3#4
  {
    \int_set:Nn \l__my_tmpa_int
      {
        \int_max:nn { \tl_count:n { #1 } } { \tl_count:n { #2 } }
         +
        \int_max:nn { \tl_count:n { #3 } } { \tl_count:n { #4 } }
      }
    \int_use:N \l__my_tmpa_int
  }

\NewDocumentCommand \myfunction { mmmm }
  { \my_function:nnnn { #1 } { #2 } { #3 } { #4 } }
\ExplSyntaxOff

\begin{document}

\myfunction{a}{bc}{def}{g}

\end{document}
You can read about the provided functions for example in interface3.

Regards
site moderator & package author
SAKDOSS
Posts: 14
Joined: Thu Feb 21, 2013 5:06 pm

Re: Convert string to integer

Post by SAKDOSS »

Thanks for the link. It seems great !
Post Reply