0

My Issue:

I have to compile a LaTeX file which contains a series of possible outcomes for a series of possible case studies. As not all the outcomes I list in the first \clistmapline have a case study, I need to control for a file to exist before creating the section. I would like to maintain the same list as the results might increase in the future, I would just like to include a control statement checking whether a file exists, and that carries on some task in the case it does (and do nothing in the case such a file does not exist).

During my analysis, I created an empty file in each directory, that I expected to use in the compilation phase as an "existence condition" to communicate LaTeX that a certain section has to be created, but I found out that an empty file is "not expandable". I do not clearly know what expandable means, what I get is that \IfFileExists does not properly work (Expandable version of \InputIfFileExists or \IfFileExists)

PSEUDO CODE: What I need is something like:

\clistmapinline{list1} {
 IfFileExists{C:/desktop/folder #1/controlfile.out} {
   \section{Section #1}
   \clistmapinline{list2}
   IfFileExists{C:/desktop/outcome/##1#1} {
      \input{##1_#1.tex}
   }
   {
   %else do nothing
   }
}

}

My code (which does not work, as it keeps returning me the errors Undefined Control Sequence and Missing \endcsname inserted)

% !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{\string_}{\textunderscore}%
}%
\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}{%
  \IfFileExists{C:/Users/Filippo/Dropbox/smc/output/SYNTH_IPC/synth_#1/non_empty_folder.out}%
  {
    \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}{%
   IfFileExists{./SITH_DATAOUT/##1_#1.tex}{  
     \subsection{Subsection ##1}%
     \input{##1_#1.tex}%
   } % IfFileexists 2 ends here
  {% ELSE 2 DO NOTHING
  }%
  } % \clistmapline 2 ends here
  }% IFFileExists 1 ends here
  { %ELSE 1 DO NOTHING
  }% 
}%clismapinline 1 ends here

\end{document}

NOTE that the files I want to input include both TeX files (as in the example) and figures (.eps) stored with composite name subsection_section.tex and .eps respectively.

(This question follows this one. Thanks to @frougon I could iterate through outcomes and case studies, what I miss is to make the IfFileExists working.)

2 Answers 2

1

Admittedly, this is a bit tricky. There are two problems with your code. Sorry to say this, but the first one is your fault. :-) You found out that the underscore can't be used in normal text to produce an underscore, because it has a special meaning for TeX. Since you use file names containing underscores 1) in file existence tests and file input commands and 2) in normal text, and you want both coming from the same input (because of the automation), you used the \myunderscore macro from an answer of Ulrich Diez. This macro expands to \string_ or to \textunderscore depending on the TeX conditional \iffilename, which was defined in Ulrich Diez's code. \string_ is fine for file existence tests and file input (though a plain underscore would be enough), and \textunderscore is fine for use in normal text. But if you want this \myunderscore macro to properly do its work, you need to use yourself \filenametrue or filenamefalse to select the appropriate kind of underscore! This can't be automatic unless you define commands that themselves use \filenametrue or filenamefalse before places where \myunderscore might be used (which my code below does). So, the problem here is that you forgot to call \filenametrue and filenamefalse in the appropriate places (i.e., before a file existence test, before file input, and before using \myunderscore in normal text).

The second problem is more difficult. The \IfFileExists macro doesn't behave very nicely when there are # characters in its then-clause or its else-clause. At the first level, you can somehow manage by doubling some of the # characters, but at the second level of \IfFileExists tests with the # character being used by two levels of \clistmapinline, this becomes really confusing. You could probably get around this by hiding the inner loop code inside a macro so as to make the inner ##1 invisible to the outer \IfFileExists. But I prefer an approach based on expl3 and \file_if_exist:nTF, because the result is more predictable regarding the number of # to use, and also more readable—it is very easy to define T, F and TF variants, which my code does.

You may now wonder what these T, F and TF variants are. Well, in your code, you needed to put a bunch of empty sets of braces {} (followed by comments to avoid too much confusion) because of the else clauses you didn't need. Well, expl3 has a better approach. Whenever you have a conditional—let's call it \iffoo:n, where the n means it takes one “normal” argument—it can have three variants:

  • \iffoo:nTF {argument} {true code} {false code}
  • \iffoo:nT {argument} {true code}
  • \iffoo:nF {argument} {false code}

If you need both the true and false branches, you use the TF variant. If you only need the true branch, you can use the T variant and spare an empty pair of braces. Similarly, if you only need the false branch, you can use the F variant. This usually produces more readable code than the old approach that always requires the {true code} and {false code} branches.

In this case, I implemented a conditional for you which has three variants: \filippo_file_if_exist:nTF, \filippo_file_if_exist:nT and \filippo_file_if_exist:nF, along with their respective document-level counterparts \MyFileIfExistTF, \MyFileIfExistT and \MyFileIfExistF. For your particular example, only the T variant is needed, but you might find the other variants useful for future work. These three macros use expl3's \file_if_exist:nTF command to do the file input without the nasty behavior of \IfFileExists regarding # characters, and also automatically call \filenametrue for you before the test, plus \filenamefalse as soon as the test outcome is known. Therefore, they solve both aforementioned problems without needing manual intervention.

In order to ease your life, I also defined a very simple \MyInput macro which begins a new TeX group, does \filenametrue inside this group, inputs the specified file and closes the group. This is useful because of the underscores, again. One caveat though: if the input file has \myunderscore in normal text, you should use \filenamefalse yourself inside the input file (I don't know if it's possible to somehow register \filenamefalse so that \input or its expl3 counterpart \file_input:n inserts this token in the input stream before the file contents). If you don't understand, it is simple: in case you use \myunderscore as normal text (i.e., for typesetting) inside the files you input, better start these files with \filenamefalse. This is because my \MyInput macro sets \filenametrue before inputing your file and can't itself write \filenamefalse inside the file:

\NewDocumentCommand \MyInput { m }
  {
    \group_begin:
    \filenametrue
    \file_input:n {#1}
    \group_end:
  }

With these things in mind, everything should go smoothly. For your future examples, please limit yourself to two or three items per level: we don't need all of AGO, ALB, ARE, ARG, ARM, AUS, AUT, AZE, BDI, BEL, BEN, BFA, BGD, BGR, BHR, BHS, BIH, BLR, BLZ, BOL, BRA, BRB, BRN, etc. to understand what you want to do! And since I have to create the test files to verify the code, simple self-explanatory names and a small number of files make things easier to prepare and also easier to understand for readers (it would be a good idea to simplify your question in order to match the items I used below, for instance).

Here is the code. :-)

\documentclass{article}
\usepackage{xparse}

\newif\iffilename               % new conditionals are initially false
\makeatletter
\newcommand\myunderscore{%
  \iffilename
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\string_}{\textunderscore}%
}%
\makeatother

\ExplSyntaxOn
\cs_new_eq:NN \clistmapinline \clist_map_inline:nn

\prg_new_protected_conditional:Npnn \filippo_file_if_exist:n #1 { T, F, TF }
  {
    \group_begin:
    \filenametrue
    \file_if_exist:nTF {#1}
      { \group_end: \prg_return_true: }
      { \group_end: \prg_return_false: }
  }

\NewDocumentCommand \MyFileIfExistTF { m }
  { \filippo_file_if_exist:nTF {#1} }

\NewDocumentCommand \MyFileIfExistT { m }
  { \filippo_file_if_exist:nT {#1} }

\NewDocumentCommand \MyFileIfExistF { m }
  { \filippo_file_if_exist:nF {#1} }

\NewDocumentCommand \MyInput { m }
  {
    \group_begin:
    \filenametrue
    \file_input:n {#1}
    \group_end:
  }
\ExplSyntaxOff

\begin{document}

\clistmapinline{ipc\myunderscore ruleoflaw, ipc\myunderscore politicalrights}
  {%
    \MyFileIfExistT{/path/to/folder #1/controlfile.out}
      {%
        \section{Synth outcome on #1}%
        \clistmapinline{AGO, ALB, ARE}
          {%
            \MyFileIfExistT{/path/to/outcome/##1_#1.tex}
              {%
                \MyInput{/path/to/outcome/##1_#1.tex}%
              }%
          }%
      }%
  }%

\end{document}

Note: the few end-of-line percent signs I have omitted in this example are all in places where they can't cause any spurious space, FYI. But feel free to add them to be on the safe side—it can't hurt and there is absolutely no problem doing so.

Here is the output when there is a controlfile.out file in both directories folder ipc_ruleoflaw and folder ipc_politicalrights, and all of the .tex files considered by the inner loop are present in directory outcome:

screenshot

If you remove for instance ARE_ipc_ruleoflaw.tex, ALB_ipc_politicalrights.tex and AGO_ipc_politicalrights.tex from directory outcome, the output becomes:

screenshot

If you further remove the file controlfile.out from directory folder ipc_ruleoflaw, you'll obtain the expected output with only one section left:

screenshot

6
  • Thank you @frougon, it worked. I think that when something does not work, is always my fault, I rarely had issues with software flaws...thank you for explaining me where I was wrong this time!! I guess I have not clear yeat how do macros work, it is an aspect I should pay more attention to! Commented Jan 21, 2020 at 20:50
  • @FilippoSanti Glad to help. If you really want to understand (La)TeX programming, my advice is to read the TeXbook. This requires some time and active brain cells, though. It is not very thick (if you exclude very technical appendices), but you may find that some chapters are much better understood after the second reading. In my case, I almost didn't practice for years after the first reading, due to life circumstances... but the second reading was really enlightening. :-) And you need practice, of course—which can be quite fun if your mind is inclined towards these games.
    – frougon
    Commented Jan 21, 2020 at 22:30
  • Yeah, I definitely need practice...thank you @frougon for the suggested reading. I have one additional, related question: I also have to include two graphics having the same naming pattern. I thought that changing slightly your code could do the trick, so i did \NewDocumentCommand \MyIncludeGraphic { m } { \group_begin: \filenametrue \includegraphics[width=0.5\textwidth] {#1} \group_end: } But the graph is not included. Could you help me sort this (hopefully last) thing out? I am really short of time! Commented Jan 22, 2020 at 6:01
  • @FilippoSanti If I add \usepackage{graphicx}, put your command definition right before \ExplSyntaxOff and insert \MyFileIfExistT{outcome/##1_#1.png}{\MyIncludeGraphic{outcome/##1_#1.png}} in the inner code block right after \MyFileIfExistT{outcome/##1_#1.tex}{\MyInput{outcome/##1_#1.tex}}, it works fine: image outcome/ALB_ipc_politicalrights.png (for instance) is included if, and only if it is present.
    – frougon
    Commented Jan 22, 2020 at 9:19
  • @FilippoSanti Adding \MyFileIfExistT{outcome/##1_#1.png}{\MyIncludeGraphic{outcome/##1_#1.png}} after \MyInput{outcome/##1_#1.tex} (i.e., inside the true clause of the \MyFileIfExistT{outcome/##1_#1.tex} test) works as well. So, maybe one of your paths is incorrect, something like that.
    – frougon
    Commented Jan 22, 2020 at 9:41
1

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}
1
  • Thank you very much. I read your answer and I think it might work. Eventually I resorted to use the one by @frougon, as it was more consistent with what I did previously...It is costing me a lot to learn how Latex works! Also thank you for signalling me the trailing commas issue! Commented Jan 21, 2020 at 20:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .