GeneralManagment of Variables in Coding

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
moon61
Posts: 5
Joined: Fri Jul 12, 2013 5:05 pm

Managment of Variables in Coding

Post by moon61 »

Hi there,

I'd appreciate some help/advice on the following:

Context: I have to generate a bunch of reports that looks all the same in term of their structure.

Objective: Automatize as much as possible the content input of each section along with the title etc.


Working example (main.tex):

Code: Select all

\documentclass[12pt,]{article}
\usepackage[english]{babel}
\usepackage{lmodern,multido}

\title{Report on xyz}
\author{moon61}

% Assuming I have 10 sections

\begin{document}
\maketitle
% Include macro file see below
\input{macro_titles.tex}

% I set a counter going till 10
\multido{\i=1+1,\n=1+1}{10}{
% I define a variable called numV that will take the % values R1 R2 etc
% I assign to numV the corresponding Ri 
\def\numV{R\i}
% Then I need to use the content of what is in numV and not numV itself....
\section{Result number \i:\numV }}
Furthermore I'd like to get the input (below) working properly for the content files. Basically They're called detailR1.tex, detailR2.tex etc...
\input{detail\numV.tex}
\end{document}

Having the file called "macro_titles.tex" looking like this

Code: Select all

\def\R1\{title one}
\def{R2\{title two}
etc...

Obviously it doesn't work as I need something like "evaluate the function \numV".

Forecast: My dream is to get the counter limit defined by the number of lines in the file called "macro_titles.tex".

Help much appreciated.
Best
Moon


PS: I've checked on the existing topics and didn't manage to identify a similar problem. If I'm wrong, I just apologize.
Last edited by localghost on Fri Jul 12, 2013 6:33 pm, edited 1 time 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.

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

Managment of Variables in Coding

Post by cgnieder »

moon61 wrote:Having the file called "macro_titles.tex" looking like this

Code: Select all

\def\R1\{title one}
\def{R2\{title two}
These definitions cannot work for various reasons but I guess you have some typos in there and actually meant to post these lines:

Code: Select all

\def\R1{title one}
\def\R2{title two}
This would still go wrong: macro names can only contain letters (in TeX's sense, that is with category code 11) or one character with a different category code. The first line would define the macro \R which must be followed by the character 1 to contain title one. The second line would redefine the macro \R so that it now must be followed by the character 2 and contains title two. The way out is two use TeX's \csname ...\endcsname primitive.

Code: Select all

\expandafter\def\csname R1\endcsname{title one}
\expandafter\def\csname R2\endcsname{title two}
More convenient would be to use a wrapper around this like LaTeX's \@namedef{<name>}{<definition>} or etoolbox' \csdef{<name>}{<definition>}.

Code: Select all

\csdef{R1}{title one}
\csdef{R2}{title two}
Macros defined this way cannot called directly but must also be called with \csname ...\endcsname or one of the wrappers \@nameuse{<name>} or \csuse{<name>}:

Code: Select all

\csuse{R2} will expand to ``title two''
I'm not entirely sure if I understood you're goal but the code below gives
loop.png
loop.png (35.22 KiB) Viewed 6203 times

Code: Select all

\documentclass[12pt,]{article}
\usepackage[english]{babel}
\usepackage{lmodern,multido}

\title{Report on xyz}
\author{moon61}

% provides
% - \whileboolexpr
% - \csdef and \csuse
% - \ifnumless
\usepackage{etoolbox}

% this can go in the macro definition file:
\csdef{R1}{title one}
\csdef{R2}{title two}
\csdef{R3}{title three}
\csdef{R4}{title four}
\csdef{R5}{title five}
\csdef{R6}{title six}
\csdef{R7}{title seven}
\csdef{R8}{title eight}
\csdef{R9}{title nine}
\csdef{R10}{title ten}

\begin{document}
\maketitle


% define a counter and loop while it is less than 11
\newcounter{step}
\whileboolexpr{ test{\ifnumless{\value{step}}{10}} }{%
  % increase counter by one
  \stepcounter{step}%
  % define \numV to contain R1, R2, ...
  \edef\numV{R\thestep}%

  \section{Result number \thestep:\numV}
  use the corresponding macro from the definition file:
  \csuse{\numV}
  % I don't have the files but this should work:
  % \input{detail\numV.tex}
}

\end{document}
Regards
site moderator & package author
moon61
Posts: 5
Joined: Fri Jul 12, 2013 5:05 pm

Managment of Variables in Coding

Post by moon61 »

Dear,

Thanks a lot for having worked on it.
You're riight I had a typo in my def (only when posting not in my source file).

Your result is similar to what I got.
In the example that you have kindely developed, we can see that the content of the section title doesn't expand into "Title one" (as an example).

1- Result number 1: R1

Instead of

1- Result number 1: Title one

Apparently the reason is that \numV is simply replaced by its content i.e (R1, for i=1) while I need numV to be replaced by "\R1" etc.

Hope I'm clear enough. If not then ready to provide more.
Many thanks again.
moon
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Managment of Variables in Coding

Post by cgnieder »

moon61 wrote:In the example that you have kindely developed, we can see that the content of the section title doesn't expand into "Title one" (as an example).
it has not been clear from your previous post that you wanted this. If you replace in my code

Code: Select all

\section{Result number \thestep:\numV}
by

Code: Select all

\section{Result number \thestep: \csuse{\numV}}
it should be what you want.
moon61 wrote:Apparently the reason is that \numV is simply replaced by its content i.e (R1, for i=1) while I need numV to be replaced by "\R1" etc.
As I tried to explain in my previous post: there is no \R1 command! Or rather: there is when you define it with one of the methods I mentioned but it can't be called directly!

Regards
site moderator & package author
moon61
Posts: 5
Joined: Fri Jul 12, 2013 5:05 pm

Re: Managment of Variables in Coding

Post by moon61 »

Excellent!

Thanks so much.
Looks obvious when I read your solution. I might have done something wrong :-(
Sorry for not have been clear enough.
Anyway thanks again.

just need to close this thread and make it as solved.
Post Reply