0

Here's my current MWE: (Note, you must save the file as lec-05.tex)

\documentclass{article}

\usepackage{currfile} \usepackage{substr}

\def\fulllecnum{ \BehindSubString{lec-}{\scantokens\expandafter{\currfilebase\noexpand}} } \newcommand\lecnum{ \IfBehindSubStringEmpty{lec-0}{\scantokens\expandafter{\currfilebase\noexpand}}{ \BehindSubString{lec-}{\scantokens\expandafter{\currfilebase\noexpand}} }{ \BehindSubString{lec-0}{\scantokens\expandafter{\currfilebase\noexpand}} } }

\setcounter{section}{\lecnum}

\begin{document} \section{HI} \end{document}

Here's the error:

! Missing number, treated as zero.
<to be read again> 
                   \let 
l.17 \setcounter{section}{\lecnum}

?

How can I convert \lecnum to an integer and then pass it to \setcounter{section}{}?

1 Answers1

0

Thanks to @David Carlisle who pointed this out.

\makeatletter
\def\@lecnum{}
\newcommand\lec[1]{%
  \ifnum #1<10 %
    \def\@lecnum{0#1}%
  \else
    \def\@lecnum{#1}%
  \fi
%
  \setcounter{section}{#1}%
  \renewcommand\thesubsection{#1.\arabic{subsection}}%
%
  \input{lectures/lec-\@lecnum.tex}%
}
\makeatother

I have files in the lectures folder named lec-01.tex, lec-02.tex, .... Then, I can just call this command like \lec{1} and it sets the section number to 1 and inputs the lectures/lec-01.tex file.

I was doing it the complicated way, first getting the lecture number from performing regex on the file name, then trying to convert that string to a number to set the section number.

David Carlisle
  • 757,742
  • TeX language is not “free-form”. Watch out for spaces, endlines and blank lines in definitions. – egreg Nov 13 '22 at 09:59