1

I have an issue when trying to use variables in a command (\ResearchModule). I read up to 8 lines from a text file and have them set to elements of an array (using code thanks to egreg!), with \ResearchModule checking to see if they are empty and properly format them. However, \ResearchModule doesn't like that I only provide the variable names and not actual values the arguments.

I've tried a few different iterations with \def, \edef, \expandafter, etc but can't get it to work properly.

Thanks for the help!

\documentclass[10pt]{article}

\usepackage{fancyhdr}
\usepackage[a4paper]{geometry}
\usepackage{tabularx}

\pagestyle{fancy}
\fancyhf{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\addtolength{\oddsidemargin}{-2cm}
\addtolength{\evensidemargin}{-2cm}
\addtolength{\textwidth}{4cm}
\addtolength{\topmargin}{-2cm}
\addtolength{\textheight}{4cm}

\raggedbottom
\raggedright
\setlength{\tabcolsep}{0cm}

\newcommand{\dates}{3cm}
\newcommand{\tabitem}{~~\llap{\textbullet}~~}

\newcommand{\AddLine}[1]{
    \ifthenelse{\isempty{#1}}{}{\tabitem #1}
    \hangindent=0.2cm
    \par
}

\newcommand{\ResearchModule}[8]{
    \begin{tabularx}{\textwidth}[t]{p{\dates}X}
        #1 & \textbf{#2} \\
        & \textit{#3} \\
    \end{tabularx}
    \hspace*{3cm}
    \begin{minipage}{.835\textwidth}
    \begin{flushleft}
        \AddLine{#4}
        \AddLine{#5}
        \AddLine{#6}
        \AddLine{#7}
        \AddLine{#8}
    \end{flushleft}
    \end{minipage}%  
}

\ExplSyntaxOn
\ior_new:N \g_hringriin_file_stream

\NewDocumentCommand{\ReadFile}{mm}
    {\hringriin_read_file:nn{#1}{#2}
        \cs_new:Npn#1##1
            {\str_if_eq:nnTF{##1}{*}
                {\seq_count:c{g_hringriin_file_\cs_to_str:N#1_seq}}
                {\seq_item:cn{g_hringriin_file_\cs_to_str:N#1_seq}{##1}}
            }
    }

\cs_new_protected:Nn\hringriin_read_file:nn
    {\ior_open:Nn \g_hringriin_file_stream{#2}
        \seq_gclear_new:c{g_hringriin_file_\cs_to_str:N#1_seq}
        \ior_map_inline:Nn\g_hringriin_file_stream{
        \seq_gput_right:cx 
            {g_hringriin_file_\cs_to_str:N#1_seq}
            {\tl_trim_spaces:n{##1}}
        }
    \ior_close:N\g_hringriin_file_stream
    }

\ExplSyntaxOff

\begin{document}

\ReadFile{\myarray}{/file.txt}

\ResearchModule
    {\myarray{1}}
    {\myarray{2}}
    {\myarray{3}}
    {\myarray{4}}
    {\myarray{5}}
    {\myarray{6}}
    {\myarray{7}}
    {\myarray{8}}

\end{document}
2
  • Welcome! Could you provide a link to egreg's code and a small example of file.txt? Right now, we can't compile your document to reproduce the issue you're facing because we'll get an error due to the missing file.
    – cfr
    Commented Jun 21 at 13:45
  • /file.txt should be file.txt unless the file really is at the root of your filesystem. and please supply a small test version of that so that people can run your example and see the problem. In general I wouldn't use \ifthenelse (even though it has my name on it) especially as you are using expl3 which already has better tests for arguments just being white space or being empty Commented Jun 21 at 13:45

1 Answer 1

0

I can't test this because you don't provide /file.txt, but the following should do. Your macro \myarray needs one full expansion to expand to the value, which is done by \ExpandArgs{e}.

I also replaced your \ifthenelse test with \IfBlankTF which is in the LaTeX kernel and the better test (though not completely equal, \IfBlankTF is also true for #1 containing spaces only).

\documentclass[10pt]{article}

\usepackage{fancyhdr}
\usepackage[a4paper]{geometry}
\usepackage{tabularx}

\pagestyle{fancy}
\fancyhf{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\addtolength{\oddsidemargin}{-2cm}
\addtolength{\evensidemargin}{-2cm}
\addtolength{\textwidth}{4cm}
\addtolength{\topmargin}{-2cm}
\addtolength{\textheight}{4cm}

\raggedbottom
\raggedright
\setlength{\tabcolsep}{0cm}

\newcommand{\dates}{3cm}
\newcommand{\tabitem}{~~\llap{\textbullet}~~}

\newcommand{\AddLine}[1]{%
  \IfBlankTF{#1}{}{\tabitem #1}%
    \hangindent=0.2cm
    \par
}

\newcommand{\ResearchModule}[8]{
    \begin{tabularx}{\textwidth}[t]{p{\dates}X}
        #1 & \textbf{#2} \\
        & \textit{#3} \\
    \end{tabularx}
    \hspace*{3cm}
    \begin{minipage}{.835\textwidth}
    \begin{flushleft}
      \ExpandArgs{e}\AddLine{#4}
      \ExpandArgs{e}\AddLine{#5}
      \ExpandArgs{e}\AddLine{#6}
      \ExpandArgs{e}\AddLine{#7}
      \ExpandArgs{e}\AddLine{#8}
    \end{flushleft}
    \end{minipage}%  
}

\ExplSyntaxOn
\ior_new:N \g_hringriin_file_stream

\NewDocumentCommand{\ReadFile}{mm}
    {\hringriin_read_file:nn{#1}{#2}
        \cs_new:Npn#1##1
            {\str_if_eq:nnTF{##1}{*}
                {\seq_count:c{g_hringriin_file_\cs_to_str:N#1_seq}}
                {\seq_item:cn{g_hringriin_file_\cs_to_str:N#1_seq}{##1}}
            }
    }

\cs_new_protected:Nn\hringriin_read_file:nn
    {\ior_open:Nn \g_hringriin_file_stream{#2}
        \seq_gclear_new:c{g_hringriin_file_\cs_to_str:N#1_seq}
        \ior_map_inline:Nn\g_hringriin_file_stream{
        \seq_gput_right:cx 
            {g_hringriin_file_\cs_to_str:N#1_seq}
            {\tl_trim_spaces:n{##1}}
        }
    \ior_close:N\g_hringriin_file_stream
    }

\ExplSyntaxOff

\begin{document}

\ReadFile{\myarray}{/file.txt}

\ResearchModule
    {\myarray{1}}
    {\myarray{2}}
    {\myarray{3}}
    {\myarray{4}}
    {\myarray{5}}
    {\myarray{6}}
    {\myarray{7}}
    {\myarray{8}}

\end{document}
1
  • 2
    Thanks for your help! This worked on my end - I'll be sure to provide additional files in the future too. Once I garner 15 reputation, I'll be back to upvote answer! Commented Jun 21 at 14:26

You must log in to answer this question.

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