TeX - LaTeX Asked by Jonasinaus on June 23, 2021
I want to add to a counter based on an ifboolexpr using etoolbox but cant get it to work as I had hoped.
I want to put various conditions throughout my document and then at the end have a count of how many times it was met through out. As I want to create a sum of the various counts at the end.
I can do the basic ones as marked as DIVCNT and MARCNT in my MWE below.
but the SPDD result wont add to the counter marked as SPDCNT.
Oddly individually it recognises the conditional but those conditionals (marked as COUNTER 1 and COUNTER 2) in the MWE output dont seem to get added to the counter
Any Suggestions as to what im doing wrong? here’s my MWE:
documentclass{article}
usepackage{etoolbox}
newcounter{divcnt}
newcommand*{divcnt}[1]{addtocounter{divcnt}{#1}}
newcommand*{totaldivcnt}{thedivcnt}
newcounter{marcnt}
newcommand*{marcnt}[1]{addtocounter{marcnt}{#1}}
newcommand*{totalmarcnt}{themarcnt}
newcounter{spdcnt}
newcommand*{spdcnt}[1]{addtocounter{spdcnt}{#1}}
newcommand*{totalspdcnt}{thespdcnt}
newcommand*{spdd}{}
newcommand*{spchk}{}
begin{document}
renewcommand{spdd}{2000}
renewcommand*{spchk}{
ifboolexpr{
test {notblank{spdd}}
}
{1}{0}
}
First Counter spdcnt{spchk}
renewcommand{spdd}{2020}
renewcommand*{spchk}{
ifboolexpr{
test {notblank{spdd}}
}
{1}{0}
}
Second Counter spdcnt{spchk}
divcnt{0}
divcnt{1}
divcnt{1}
divcnt{0}
marcnt{1}
marcnt{1}
marcnt{1}
marcnt{1}
marcnt{1}
Total MAR Count: totalmarcnt{}
Total DIV Count: totaldivcnt{}
Total SPD Count: totalspdcnt{}
Sum of discrepancies: thenumexprthemarcnt-thedivcnt-thespdcntrelax
end{document}
The macro spdd
that you check for being blank is a command (defined with newcommand
), therefore you can use the ifdefempty
test from etoolbox
. This test already provides a True and False part, so you don't need ifboolexpr
with a test
. This simplified macro is also suitable to use as an argument for the counter increment macro.
Another issue is that you define the macro spchk
two times, with exactly the same definition. This is redundant, you can define it once at the start of the document and use it any number of times later.
MWE:
documentclass{article}
usepackage{etoolbox}
newcounter{divcnt}
newcommand*{divcnt}[1]{addtocounter{divcnt}{#1}}
newcommand*{totaldivcnt}{thedivcnt}
newcounter{marcnt}
newcommand*{marcnt}[1]{addtocounter{marcnt}{#1}}
newcommand*{totalmarcnt}{themarcnt}
newcounter{spdcnt}
newcommand*{spdcnt}[1]{addtocounter{spdcnt}{#1}}
newcommand*{totalspdcnt}{thespdcnt}
newcommand*{spdd}{}
newcommand*{spchk}{}
begin{document}
renewcommand*{spchk}{%
ifdefempty{spdd}{0}{1}%
}
renewcommand{spdd}{2000}
First Counter spdcnt{spchk}
renewcommand{spdd}{2020}
Second Counter spdcnt{spchk}
divcnt{0}
divcnt{1}
divcnt{1}
divcnt{0}
marcnt{1}
marcnt{1}
marcnt{1}
marcnt{1}
marcnt{1}
Total MAR Count: totalmarcnt{}
Total DIV Count: totaldivcnt{}
Total SPD Count: totalspdcnt{}
Sum of discrepancies: thenumexprthemarcnt-thedivcnt-thespdcntrelax
end{document}
Result:
Correct answer by Marijn on June 23, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP