1

I am using \usepackage{verbatim} to show a .txt file in my LaTeX document, via \verbatiminput{<path to .txt>}. And this works fine. However, the data that I display looks like this:

5.35    0.35    5.61    -0.39   6.84    -0.16   
5.45    0.45    5.52    -0.48   6.84    -0.16   
5.45    0.45    5.51    -0.49   6.96    -0.04   
5.45    0.45    5.63    -0.37   6.96    -0.04   
5.47    0.47    5.63    -0.37   6.96    -0.04   
5.45    0.45    5.63    -0.37   6.84    -0.16       
5.35    0.35    5.63    -0.37   6.84    -0.16   
5.47    0.47    5.51    -0.49   6.96    -0.04

I would like to have a colour scheme: I want every odd column to have a light-gray background. And I would like this table to be centered as well. Is there a way to achieve this?

3

You can read in your table and process it as a tabular after splitting each line at spaces, which are next substituted with &.

\begin{filecontents*}{\jobname-1.dat}
5.35    0.35    5.61    -0.39   6.84    -0.16   
5.45    0.45    5.52    -0.48   6.84    -0.16   
5.45    0.45    5.51    -0.49   6.96    -0.04   
5.45    0.45    5.63    -0.37   6.96    -0.04   
5.47    0.47    5.63    -0.37   6.96    -0.04   
5.45    0.45    5.63    -0.37   6.84    -0.16       
5.35    0.35    5.63    -0.37   6.84    -0.16   
5.47    0.47    5.51    -0.49   6.96    -0.04
\end{filecontents*}
\begin{filecontents*}{\jobname-2.dat}
5.35    0.35    5.61    -0.39   6.84
5.45    0.45    5.52    -0.48   6.84
5.45    0.45    5.51    -0.49   6.96
5.45    0.45    5.63    -0.37   6.96
5.47    0.47    5.63    -0.37   6.96
5.45    0.45    5.63    -0.37   6.84
5.35    0.35    5.63    -0.37   6.84
5.47    0.47    5.51    -0.49   6.96
\end{filecontents*}

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\tableinput}{m}
 {
  \tableinput_main:n { #1 }
 }

\ior_new:N \g__tableinput_file_ior
\tl_new:N \l__tableinput_body_tl
\seq_new:N \l__tableinput_row_seq

\cs_new_protected:Nn \tableinput_main:n
 {
  \tl_clear:N \l__tableinput_body_tl
  \ior_open:Nn \g__tableinput_file_ior { #1 }
  \ior_map_inline:Nn \g__tableinput_file_ior
   {
    \seq_set_split:Nnn \l__tableinput_row_seq { ~ } { ##1 }
    \seq_pop_right:NN \l__tableinput_row_seq \l_tmpa_tl % last item is blank
    \tl_put_right:Nx \l__tableinput_body_tl { \seq_use:Nn \l__tableinput_row_seq { & } }
    \tl_put_right:Nn \l__tableinput_body_tl { \\ }
   }
  \ior_close:N \g__tableinput_file_ior
  \begin{center}\ttfamily
  \int_if_odd:nTF { \seq_count:N \l__tableinput_row_seq }
   {
    \begin{tabular}
     {
      *{ \int_eval:n { (\seq_count:N \l__tableinput_row_seq - 1) /2 } }
       {>{\columncolor{gray!30}}r r}
      >{\columncolor{gray!30}}r
     }
   }
   {
    \begin{tabular}
     {
      *{ \int_eval:n { (\seq_count:N \l__tableinput_row_seq) /2 } }
       {>{\columncolor{gray!30}}r r}
     }
   }
   \tl_use:N \l__tableinput_body_tl
   \end{tabular}
   \end{center}
 }

\ExplSyntaxOff

\begin{document}

\tableinput{\jobname-1.dat}

\tableinput{\jobname-2.dat}

\end{document}

enter image description here

1

If you are willing to change to the listings package you can use the add-on package lstlinebgrd as follows:

\begin{filecontents}{myfile.txt}
5.35    0.35    5.61    -0.39   6.84    -0.16   
5.45    0.45    5.52    -0.48   6.84    -0.16   
5.45    0.45    5.51    -0.49   6.96    -0.04   
5.45    0.45    5.63    -0.37   6.96    -0.04   
5.47    0.47    5.63    -0.37   6.96    -0.04   
5.45    0.45    5.63    -0.37   6.84    -0.16       
5.35    0.35    5.63    -0.37   6.84    -0.16   
5.47    0.47    5.51    -0.49   6.96    -0.04
\end{filecontents}

\documentclass{article}

\usepackage{listing}
\usepackage{lstlinebgrd}
\definecolor{mylightgray}{RGB}{220, 220, 220}

\begin{document}
\lstinputlisting[linebackgroundcolor={\ifodd\value{lstnumber}\color{mylightgray}\fi}]{myfile.txt}
\end{document}

enter image description here

  • thx so much, leandriis! This looks so much better already. But now every odd row / line is colored. I would like to color every odd column. I was google searching a columnbackgroundcolor command but I think this does not exist – user503842 Jul 7 at 13:18
  • 1
    @user503842: I completely overlooked the "column" in your question and somehow thought you were interested in coloring rows instead. Maybe you can use the datatool package to achieve the output you are looking for. – leandriis Jul 7 at 13:53

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.