There are some problems with your code:
Before using \myunderscore
you need to make sure that the switch \iffilename
is switched properly.
\filenametrue
indicates that subsequent instances of \myunderscore
are used for creating underscores which are part of a file-name which in turn is part of a directive for something that gets carried out on your system/machine, i.e., \IfFileExists
or \input
.
\filenamefalse
indicates that subsequent instances of \myunderscore
are used for placing textual phrases into the .pdf-file/.dvi-file which contain underscores.
Look at the sequence:
\clistmapinline{AGO, ALB, ARE, ARG, ARM, AUS, AUT, AZE, BDI, BEL, BEN, BFA, BGD, BGR, BHR, BHS, BIH, BLR, BLZ, BOL, BRA, BRB, BRN, BTN, BWA, CAF, CAN, CHE, CHL, CHN, CIV, CMR, COD, COG, COL, COM, CPV, CRI, CYP, CZE, DEU, DJI, DNK, DOM, DZA, ECU, EGY, ESP, EST, ETH, FIN, FJI, FRA, GAB, GBR, GEO, GHA, GIN, GMB, GNB, GNQ, GRC, GRD, GTM, HND, HRV, HTI, HUN, IDN, IND, IRL, IRN, IRQ, ISL, ISR, ITA, JAM, JOR, JPN KAZ KEN KGZ KHM KOR KWT LAO LBN LBR LCA LKA, LSO, LTU, LUX, LVA, MAR, MDA, MDG, MDV, MEX, MKD, MLI, MLT, MMR, MNG, MOZ, MRT, MUS, MWI, MYS, NAM, NER, NGA, NIC NLD NOR NPL NZL OMN PAK PAN PER PHL POL PRT, PRY, QAT, ROU, RUS, RWA, SAU, SEN, SGP, SLE, SLV, SRB, STP, SUR, SVK, SVN, SWE, SWZ, SYC, SYR, TCD, TGO, THA, TJK, TKM, TTO, TUN, TUR, TWN, TZA, UGA, UKR, URY, USA, UZB, VCT, VEN, VNM, YEM, ZAF, ZMB, ZWE}
Seems with many items trailing commas are missing.
E.g., the partial sequence JPN KAZ KEN KGZ KHM KOR KWT LAO LBN LBR LCA LKA,
is taken for one single element of the list.
I suppose this is not intended. Be that as it may. In any case this causes problems: That partial sequence is taken for a component of a file-name. That component contains spaces. Therefore \IfFileExist
will have to deal with a file-name that contains spaces. This causes all kinds of undesired effects. On some platforms it sort of works as long as the entire filename+filepath is wrapped in double quotes ("
):
% !TEX root = C:\Users\Filippo\Dropbox\smc\output\_report\loop_report.tex
\documentclass{article}
\usepackage{natbib}
\usepackage{expl3}
\usepackage{setspace}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{bookmark}
\ExplSyntaxOn
\cs_new_eq:NN \clistmapinline \clist_map_inline:nn
\ExplSyntaxOff
% -----------------------------------------
\newif\iffilename
\global\filenamefalse
\makeatletter
\newcommand\myunderscore{%
\iffilename
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi{_}{\textunderscore}%
}%
\newcommand\firstofone[1]{#1}%
\newcommand\noneofone[1]{}%
\makeatother
% -----------------------------------------
\begin{document}
\clistmapinline{%
ipc\myunderscore ruleoflaw, ipc\myunderscore politicalrights, ipc\myunderscore gender,
ipc\myunderscore socprotection, ipc\myunderscore business, ipc\myunderscore health,
ipc\myunderscore work, ipc\myunderscore edu, ipc\myunderscore security,
ipc\myunderscore environment, ipc\myunderscore minority, ipc\myunderscore cvrights,
ipc\myunderscore womenrights, ipc\myunderscore corrupt%
}{%
\global\filenametrue
\IfFileExists{%
"C:/Users/Filippo/Dropbox/smc/output/SYNTH_IPC/synth_#1/non_empty_folder.out"%
}%
{%
\global\filenamefalse
\section{SYNTH OUTCOME ON #1}%
\clistmapinline{%
AGO, ALB, ARE, ARG, ARM, AUS, AUT, AZE, BDI, BEL, BEN, BFA, BGD, BGR, BHR, BHS, BIH,
BLR, BLZ, BOL, BRA, BRB, BRN, BTN, BWA, CAF, CAN, CHE, CHL, CHN, CIV, CMR, COD, COG,
COL, COM, CPV, CRI, CYP, CZE, DEU, DJI, DNK, DOM, DZA, ECU, EGY, ESP, EST, ETH, FIN,
FJI, FRA, GAB, GBR, GEO, GHA, GIN, GMB, GNB, GNQ, GRC, GRD, GTM, HND, HRV, HTI, HUN,
IDN, IND, IRL, IRN, IRQ, ISL, ISR, ITA, JAM, JOR, JPN, KAZ, KEN, KGZ, KHM, KOR, KWT,
LAO, LBN, LBR, LCA, LKA, LSO, LTU, LUX, LVA, MAR, MDA, MDG, MDV, MEX, MKD, MLI, MLT,
MMR, MNG, MOZ, MRT, MUS, MWI, MYS, NAM, NER, NGA, NIC, NLD, NOR, NPL, NZL, OMN, PAK,
PAN, PER, PHL, POL, PRT, PRY, QAT, ROU, RUS, RWA, SAU, SEN, SGP, SLE, SLV, SRB, STP,
SUR, SVK, SVN, SWE, SWZ, SYC, SYR, TCD, TGO, THA, TJK, TKM, TTO, TUN, TUR, TWN, TZA,
UGA, UKR, URY, USA, UZB, VCT, VEN, VNM, YEM, ZAF, ZMB, ZWE
}{%
\global\filenametrue
\IfFileExists{"./SITH_DATAOUT/##1_#1.tex"}%
{%
\global\filenamefalse
\subsection{Subsection ##1}%
\global\filenametrue
\input{"./SITH_DATAOUT/##1_#1.tex"}%
}{}% Arguments of second \IfFileExists end here.
}% Second \clistmapinline ends here.
}{}% Arguments of first \IfFileExists end here.
}% First \clistmapinline ends here.
\global\filenamefalse
\end{document}
The way \IfFileExists
works can cause other problems as well: \IfFileExists
internally defines temporary macros from its second and third argument. If these arguments contain consecutive hashes, the amount of these hashes might get halved at the time of expanding these temporary macros. Therefore I suggest not putting things directly into the second and third argument but using \firstofone
/\noneofone
:
% !TEX root = C:\Users\Filippo\Dropbox\smc\output\_report\loop_report.tex
\documentclass{article}
\usepackage{natbib}
\usepackage{expl3}
\usepackage{setspace}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{bookmark}
\ExplSyntaxOn
\cs_new_eq:NN \clistmapinline \clist_map_inline:nn
\ExplSyntaxOff
% -----------------------------------------
\newif\iffilename
\global\filenamefalse
\makeatletter
\newcommand\myunderscore{%
\iffilename
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi{_}{\textunderscore}%
}%
\newcommand\firstofone[1]{#1}%
\newcommand\noneofone[1]{}%
\makeatother
% -----------------------------------------
\begin{document}
\clistmapinline{%
ipc\myunderscore ruleoflaw, ipc\myunderscore politicalrights, ipc\myunderscore gender,
ipc\myunderscore socprotection, ipc\myunderscore business, ipc\myunderscore health,
ipc\myunderscore work, ipc\myunderscore edu, ipc\myunderscore security,
ipc\myunderscore environment, ipc\myunderscore minority, ipc\myunderscore cvrights,
ipc\myunderscore womenrights, ipc\myunderscore corrupt%
}{%
\global\filenametrue
\IfFileExists{%
"C:/Users/Filippo/Dropbox/smc/output/SYNTH_IPC/synth_#1/non_empty_folder.out"%
}%
{\firstofone}{\noneofone}%
{%
\global\filenamefalse
\section{SYNTH OUTCOME ON #1}%
\clistmapinline{%
AGO, ALB, ARE, ARG, ARM, AUS, AUT, AZE, BDI, BEL, BEN, BFA, BGD, BGR, BHR, BHS, BIH,
BLR, BLZ, BOL, BRA, BRB, BRN, BTN, BWA, CAF, CAN, CHE, CHL, CHN, CIV, CMR, COD, COG,
COL, COM, CPV, CRI, CYP, CZE, DEU, DJI, DNK, DOM, DZA, ECU, EGY, ESP, EST, ETH, FIN,
FJI, FRA, GAB, GBR, GEO, GHA, GIN, GMB, GNB, GNQ, GRC, GRD, GTM, HND, HRV, HTI, HUN,
IDN, IND, IRL, IRN, IRQ, ISL, ISR, ITA, JAM, JOR, JPN, KAZ, KEN, KGZ, KHM, KOR, KWT,
LAO, LBN, LBR, LCA, LKA, LSO, LTU, LUX, LVA, MAR, MDA, MDG, MDV, MEX, MKD, MLI, MLT,
MMR, MNG, MOZ, MRT, MUS, MWI, MYS, NAM, NER, NGA, NIC, NLD, NOR, NPL, NZL, OMN, PAK,
PAN, PER, PHL, POL, PRT, PRY, QAT, ROU, RUS, RWA, SAU, SEN, SGP, SLE, SLV, SRB, STP,
SUR, SVK, SVN, SWE, SWZ, SYC, SYR, TCD, TGO, THA, TJK, TKM, TTO, TUN, TUR, TWN, TZA,
UGA, UKR, URY, USA, UZB, VCT, VEN, VNM, YEM, ZAF, ZMB, ZWE
}{%
\global\filenametrue
\IfFileExists{"./SITH_DATAOUT/##1_#1.tex"}%
{\firstofone}{\noneofone}%
{%
\global\filenamefalse
\subsection{Subsection ##1}%
\global\filenametrue
\input{"./SITH_DATAOUT/##1_#1.tex"}%
}% Argument of second \firstofone/\noneofone ends here.
}% Second \clistmapinline ends here.
}% Argument of first \firstofone/\noneofone ends here.
}% First \clistmapinline ends here.
\global\filenamefalse
\end{document}
Probably you can omit one \IfFileExists
-test by impelenting another \if
-switch which within the second loop indicates whether a \section
was already placed:
% !TEX root = C:\Users\Filippo\Dropbox\smc\output\_report\loop_report.tex
\documentclass{article}
\usepackage{natbib}
\usepackage{expl3}
\usepackage{setspace}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{bookmark}
\ExplSyntaxOn
\cs_new_eq:NN \clistmapinline \clist_map_inline:nn
\ExplSyntaxOff
% -----------------------------------------
\newif\iffilename
\global\filenamefalse
\makeatletter
\newcommand\myunderscore{%
\iffilename
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi{_}{\textunderscore}%
}%
\newcommand\firstofone[1]{#1}%
\newcommand\noneofone[1]{}%
\newif\ifSectionAlreadyPlaced
\global\SectionAlreadyPlacedfalse
\makeatother
% -----------------------------------------
\begin{document}
\clistmapinline{%
ipc\myunderscore ruleoflaw, ipc\myunderscore politicalrights, ipc\myunderscore gender,
ipc\myunderscore socprotection, ipc\myunderscore business, ipc\myunderscore health,
ipc\myunderscore work, ipc\myunderscore edu, ipc\myunderscore security,
ipc\myunderscore environment, ipc\myunderscore minority, ipc\myunderscore cvrights,
ipc\myunderscore womenrights, ipc\myunderscore corrupt%
}{%
\global\SectionAlreadyPlacedfalse
\clistmapinline{%
AGO, ALB, ARE, ARG, ARM, AUS, AUT, AZE, BDI, BEL, BEN, BFA, BGD, BGR, BHR, BHS, BIH,
BLR, BLZ, BOL, BRA, BRB, BRN, BTN, BWA, CAF, CAN, CHE, CHL, CHN, CIV, CMR, COD, COG,
COL, COM, CPV, CRI, CYP, CZE, DEU, DJI, DNK, DOM, DZA, ECU, EGY, ESP, EST, ETH, FIN,
FJI, FRA, GAB, GBR, GEO, GHA, GIN, GMB, GNB, GNQ, GRC, GRD, GTM, HND, HRV, HTI, HUN,
IDN, IND, IRL, IRN, IRQ, ISL, ISR, ITA, JAM, JOR, JPN, KAZ, KEN, KGZ, KHM, KOR, KWT,
LAO, LBN, LBR, LCA, LKA, LSO, LTU, LUX, LVA, MAR, MDA, MDG, MDV, MEX, MKD, MLI, MLT,
MMR, MNG, MOZ, MRT, MUS, MWI, MYS, NAM, NER, NGA, NIC, NLD, NOR, NPL, NZL, OMN, PAK,
PAN, PER, PHL, POL, PRT, PRY, QAT, ROU, RUS, RWA, SAU, SEN, SGP, SLE, SLV, SRB, STP,
SUR, SVK, SVN, SWE, SWZ, SYC, SYR, TCD, TGO, THA, TJK, TKM, TTO, TUN, TUR, TWN, TZA,
UGA, UKR, URY, USA, UZB, VCT, VEN, VNM, YEM, ZAF, ZMB, ZWE
}{%
\global\filenametrue
\IfFileExists{"./SITH_DATAOUT/##1_#1.tex"}%
{\firstofone}{\noneofone}{}%
{%
\global\filenamefalse
\allowbreak % something that may trigger a page-break before the following
% \section/\subsection.
\ifSectionAlreadyPlaced
\else
\global\SectionAlreadyPlacedtrue
%\uppercase{%
\section{SYNTH OUTCOME ON #1}%
%}%
\fi
\subsection{Subsection ##1}%
\global\filenametrue
\input{"./SITH_DATAOUT/##1_#1.tex"}%
}% Argument of \firstofone/\noneofone ends here.
}% Second \clistmapinline ends here.
}% First \clistmapinline ends here.
\global\filenamefalse
\end{document}