How to create command that creates new if command?

I want to load parameters (for conditional compilation) from files. I wrote command param that creates new if command and read parameter from file, containing following line:

\paramnametrue % or false


My param command:

\newcommand{\param}[1]{\newif\if#1 \IfFileExists{#1}{\input{#1}}{}}


The problem is that when I write

\param{A}


outside of document environment I get

! LaTeX Error: Missing \begin{document}.


and inside of document environment I get

A


inside of my document. How can I make it work? How can I create not handling text commands?

You can't build a control sequence name by simply typing \if#1, because \if is a single token.

You have a couple of possibilities. First one without packages:

\newcommand{\param}[1]{%
\expandafter\newif\csname if#1\endcsname
\IfFileExists{#1}{\input{#1}}{}%
}


This has the disadvantage that no check is performed if the conditional already exists. A second possibility is with etoolbox:

\usepackage{etoolbox}
\newcommand{\param}[1]{%
\newboolean{#1}%
\IfFileExists{#1}{\input{#1}}{}%
}


What \newboolean does is similar to \newif, so after \param{A} the conditional \ifA and the commands \Atrue and \Afalse will be defined. The same happens with the former solution, but here also a check for duplicate definitions is performed. If you don't want the check, then use \provideboolean.

If #1 is A then \if#1 is not the single token \ifA it is the two tokens \if A so you end up redefining \if which is not good. You want

\expandafter\newif\csname if#1\endcsname


to construct the \ifA token and pass it to \newif

• That was my idea, but I was trying it at tlhiv.org/ltxpreview and this: \newcommand{\param}[1]{\expandafter\newif\csname if#1\endcsname} \param{A} produces the error ! Missing control sequence inserted. inaccessible, at least at this previewer. Why? Jun 26 '13 at 14:49
• @JLDiaz no idea, presumably the document it sends to latex is not the document it shows you. The code in your comment works in latex. Jun 26 '13 at 15:02