0

I am trying to use the suggested answer to passing arguments to a LaTeX document, posted here as solution#Three.

I am passing the argument as follows in my Windows Command Prompt:

C:\Users\afp\projects>pdflatex "\def\InputFilename{sample}\input{myLaTeXDoc.tex}"

In case the argument is not provided, I would like to have a default file to process. However, the compiler still complains about Undefined control sequence. \begin{filecontents*}{\FILENAME.csv}. I am not able to neither

  • pass the argument at the command prompt nor
  • generate a default csv file.

Could someone please help me with that?

\documentclass[tikz, border=0mm]{standalone}

\usepackage{array}
\usepackage{readarray}
\readarraysepchar{,}

\usepackage{filecontents}

% If the argument is provided at the Windows command prompt, define \FILENAME as the input argument 
% otherwise define \FILENAME as a default comma-separated file to process.
\ifdefined\InputFilename
    \def\FILENAME{\InputFilename} 
\else
    \begin{filecontents*}{\FILENAME.csv}
        1,2,3
        4,5,6
        7,8,9
        10,11,12
    \end{filecontents*}
\fi

\begin{document}
    \wlog{filename is \FILENAME.csv}
    \readdef{\FILENAME.csv}\namedata
    \readarray\namedata\mydata[-,\ncols]
    \wlog{\FILENAME.csv has \nrows rows and \ncols columns.}
\end{document}
  • 1
    you have not defined \FILENAME anywhere. – David Carlisle Aug 2 at 6:15
  • @DavidCarlisle wouldn't \ifdefined\InputFilename \def\FILENAME{\InputFilename} \else \begin{filecontents*}{\FILENAME.csv} 1,2,3 4,5,6 7,8,9 10,11,12 \end{filecontents*} \fi define \FILENAME? – AFP Aug 2 at 6:17
  • 1
    not in the \else case no, what name do you want to write from filecontents? – David Carlisle Aug 2 at 6:18
1

You do not define \FILENAME in the case that filecontents* is used, presumably you want something like this that defines it to be \jobname

\documentclass[tikz, border=0mm]{standalone}

\usepackage{array}
\usepackage{readarray}
\readarraysepchar{,}

\usepackage{filecontents}

% If the argument is provided at the Windows command prompt use it 
% otherwise create a default comma-separated file to process.
\ifdefined\InputFilename
    \def\FILENAME{\InputFilename} 
\else
    \def\FILENAME{\jobname} 
    \begin{filecontents*}{\FILENAME.csv}
        1,2,3
        4,5,6
        7,8,9
        10,11,12
    \end{filecontents*}
\fi

\begin{document}
    \wlog{filename is \FILENAME.csv}
    \readdef{\FILENAME.csv}\namedata
    \readarray\namedata\mydata[-,\ncols]
    \wlog{\FILENAME.csv has \nrows rows and \ncols columns.}
\end{document}
  • Thank you. it runs with no issue with the default file. However, I am still having issue running it with arguments at the command prompt. No matter what I input the log file always has myLaTeXDoc.csv has 4rows and 3columns. Seems like \ifdefined\InputFilename is never true although I am using pdflatex "\def\InputFilename{sample}\input{myLaTeXDoc.tex}". – AFP Aug 2 at 6:28
  • 1
    @AFP I am not sure, on the windows command line, do you need the quotes? I don't have a windows tex to test. you can add \show\InputFilename to the start of your document and it will stop as if for an error and show the current meaning of \InputFilename so you can see if it is being defined. presumably you have a sample.csv if you are passing in sample ? – David Carlisle Aug 2 at 6:37
  • The compiler won't like it without the quote as pdflatex \def\InputFilename{sample}\input{myLaTeXDoc.tex}. and yes, the file sample.csv exists in the same directory. – AFP Aug 2 at 6:46
  • 1
    @AFP hmm then I can't see why it doesn't work, can you show the log with and without \def\InputFilename{sample} ? I get filename is sample.csv sample.csv has 2rows and 4columns. – David Carlisle Aug 2 at 6:52
  • 1
    I got an undefined command error for \nrows if there was no sample.csv @AFP – David Carlisle Aug 2 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.