Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3301338
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:44:30+00:00 2026-05-17T20:44:30+00:00

I’m trying to write a command in LaTeX that takes a string such as

  • 0

I’m trying to write a command in LaTeX that takes a string such as 8:00A and converts it into the number of minutes, as part of a script to draw a class schedule using TikZ. However, I’m running into some issues – it seems that LaTeX doesn’t actually evaluate the contents of a command.

My command is currently:

\newcommand{\timetominutes}[1]{
  \IfSubStr{#1}{P}{720}{0}+\IfSubStr{#1}{P}{\StrBetween{#1}{:}{P}}{\StrBetween{#1}{:}{A}}+60*\StrBefore{#1}{:}
}

If you print out the text from it, it will correctly calculate the number of minutes from midnight. However, if used inside another function, it becomes apparent that it doesn’t actually run any of those commands – it merely returns text including those commands. So if I write:

\myfunc{\timetominutes{8:00A}}

Instead of \myfunc seeing something useful like 0+00+60*8, it sees \IfSubStr{8:00A}{P}{720}{0}+\IfSubStr{8:00A}{P}{\StrBetween{8:00A}{:}{P}}{\StrBetween{8:00A}{:}{A}}+60*\StrBefore{8:00A}{:}. This is absolutely useless to me and I can’t seen to find a way to force LaTeX to execute subcommands before the main one. I assume there’s a way to do it, but LaTeX documentation is scarce and I can’t seem to find anything.

Alternatively, if there’s a way to get LaTeX to stop complaining about too many }s (when I have the correct number), that could work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:44:30+00:00Added an answer on May 17, 2026 at 8:44 pm

    Unfortunately, no. As the xstring package states:

    The macros of this package are not purely expandable

    (Section 3.2 of xtring_doc_en.pdf.)

    This “expandable” concept, if you’re not familiar with it, is quite a hairy subject in TeX. Simply put, something that is not expandable cannot be evaluated as an argument. Anything that uses an assignment somewhere is guaranteed not to be expandable in most TeX varieties, but other non-expandable triggers exist as well. The solution to such problems is rather difficult for anyone that’s not familiar with the inner workings of TeX’s “mouth” (the part of TeX that handles things like expansion).

    Hint: If the LaTeX code is generated by a script: use the script to convert the time expressions, because just about any programming language is easier to use than TeX when it comes to string manipulation. (Or just about anything else for that matter.)


    The xstring package does hint at a way out: You can store the result of most operations in a variable, by adding [\variable] to the end of the calls. This means you’d need to rewrite \timetominutes to something that builds up the result piece by piece, and then store that result in a command sequence for use later on.

    Usage:

    \timetominutesinto\somevar{8:00A} % \somevar contains 48
    \expandafter\myfunc\expandafter{\somevar} % calls \myfunc{48}
    

    Note the use of \expandafter, which tells TeX to do a simple one-level expansion (evaluation) of a command sequence after the next. If you didn’t use the two \expandafters, you’d get \somevar as an argument to \myfunc, not 48.

    (Caution: ugly TeX code ahead!)

    \makeatletter % allow @ in command names
    \def\timetominutesinto#1#2{% 
      % #1 = command to store the result in
      % #2 = the text to parse
      % \ttm@tempa and \ttm@tempb are temporary variables for this macro
      \let\ttm@tempa\empty % make the command empty
      \IfSubStr{#2}{P}{%
        \def\ttm@tempa{720+}% set tempa to "720+"
        \StrBetween{#2}{:}{P}[\ttm@tempb]% store substring between : and P into tempb
        \edef\ttm@tempa{\ttm@tempa \ttm@tempb}% set tempa to tempa + tempb
      }{%
        \def\ttm@tempa{0+}% set tempa to 0+
        \StrBetween{#2}{:}{A}[\ttm@tempb]% store substring between : and A into tempb
        \edef\ttm@tempa{\ttm@tempa \ttm@tempb}% set tempa to tempa + tempb
      }%
      \edef\ttm@tempa{\ttm@tempa+60*}% set tempa to tempa + "+60*"
      \StrBefore{#2}{:}[\ttm@tempb]% store substring before : into tempb
      % now, set #1 to the result of evaluating the formula returned by concatenating
      % tempa and tempb
      \edef#1{\numexpr \ttm@tempa \ttm@tempb}%
    }
    

    \def is the TeX primitive corresponding to LaTeX’s \newcommand/\renewcommand. \edef means Expanding \def, which evaluates the definition before assigning the result to a command sequence. \numexpr evaluates a simple number expression, like x + m + h * 60 created by the command above.

    It is also possible to calculate the result immediately as a number, without building up the formula, by using integer arithmetic. But that would make the code even more remote from your original intent.

    It is possible to do these string manipulations through TeX itself, without using the xstring package (even expandible in this particular case). But that’s pretty low level stuff, which cannot easily be repeated if you’re not a TeX wizzard.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.