1

I am using the csvsimple package to read a csv, and am doing some minimal formatting in Latex. The csv consists of strings formatted as percentages.

I have created hacks to get around most of the problems, but now have a % symbol off to the side of my table. Here is my code:

\documentclass[titlepage]{report}
\usepackage{graphicx}    
\usepackage{csvsimple} % Generates table from .csv

\newcommand{\csvloopx}[2][]{\csvloop{#1,#2}}
\newcommand{\csvautotabularx}[2][]{\csvloopx[#1]{autotabular={#2}}}
\newcommand{\respectpercent}{\catcode`\%=12\relax}

\begin{document}



\begin{table}[H]

    \begin{center}
    \begingroup\catcode`"=9
     \csvreader[no head, tabular=|c|r|r|r|r|,%
        before table=\respectpercent,%
        table head=\hline \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Year}%
                 & \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Base}% 
                 & \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Round Lot \\ of 100 \\ Shares}% 
                 & \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per~Share}%  
                 & \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per Share and \\ Round Lot \\ of 100 Shares}\\\hline,% 
        late after line = \\\hline,%
            filter expr={
                test{\ifnumgreater{\thecsvinputline}{1}}}]%
        {Figures/Document/Cost.csv}%
        {1=\one, 2=\two, 3=\three, 4=\four, 5=\five}%
        {$\one$ & $\two$ & $\three$ & $\four$ & $\five$}%
    \endgroup
    \end{center}

    \caption{Market Cost Assumption}
    \label{ tab:Slippage}
\end{table}


\end{document}

and the resulting table is

enter image description here

Any help is much appreciated. Thanks.

Here is the head of the csv file:

"Year","Base","RoundLot","TransactionCost","TransactionRoundLot"
2001,"3.41%","3.42%","3.35%","3.35%"
2002,"-10.46%","-10.45%","-10.50%","-10.49%"
2003,"22.58%","22.57%","22.53%","22.52%"
2004,"20.03%","20.02%","19.98%","19.97%"
  • 2
    Your code is not compilable. – Michael Fraiman Jun 19 '17 at 20:31
  • Also, you should almost never use the H option. Why? See here: tex.stackexchange.com/questions/370627/… – Moriambar Jun 19 '17 at 20:31
  • I have already found that removing the comment symbol from the line {$\one$ & $\two$ & $\three$ & $\four$ & $\five$}% is solving the problem... but I am looking to find why this happens – koleygr Jun 19 '17 at 20:50
6

The problem happens because you tell LaTeX that % is not a comment character. Removing all of them (none was necessary in the code) fixes the issue.

\begin{filecontents*}{\jobname.csv}
"Year","Base","RoundLot","TransactionCost","TransactionRoundLot"
2001,"3.41%","3.42%","3.35%","3.35%"
2002,"-10.46%","-10.45%","-10.50%","-10.49%"
2003,"22.58%","22.57%","22.53%","22.52%"
2004,"20.03%","20.02%","19.98%","19.97%"
\end{filecontents*}

\documentclass[titlepage]{report}
\usepackage{csvsimple} % Generates table from .csv

\newcommand{\respectpercent}{\catcode`\%=12\relax}
\newcommand{\ignoredblquote}{\catcode`\"=9\relax}

\begin{document}

\begin{table}
\centering

\csvreader[
  no head,
  tabular=|c|r|r|r|r|,
  before table=\respectpercent\ignoredblquote,
  table head={
    \hline
    \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Year} &
    \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Base} &
    \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Round Lot \\ of 100 \\ Shares} &
    \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per~Share} &
    \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per Share and \\ Round Lot \\ of 100 Shares} \\
    \hline
  },
  late after line = \\\hline,
  filter expr={
    test{\ifnumgreater{\thecsvinputline}{1}}
  },
]{\jobname.csv}%
  {1=\one, 2=\two, 3=\three, 4=\four, 5=\five}
  {$\one$ & $\two$ & $\three$ & $\four$ & $\five$}

\caption{Market Cost Assumption}
\label{tab:Slippage}

\end{table}

\end{document}

I changed the name of the csv file just to make the example self contained. I also removed unused packages and commands for the same reason.

Avoid the [H] option. I also defined \ignoredblquote similarly to \respectpercent, so a group is not needed.

Note that a space in \label is not ignored.

enter image description here

  • The only % that causes the problem is the one after $\five$} .... All the other changes are good for the MWE but doesn't change the result... I can not understand why at that point a percent couses this problem... If you leave a space... it goes under the table... it has to do with the way csvsimple "closes" the table – koleygr Jun 19 '17 at 21:23
0

Rescue service:

\begin{filecontents*}{\jobname.csv}
"Year","Base","RoundLot","TransactionCost","TransactionRoundLot"
2001,"3.41%","3.42%","3.35%","3.35%"
2002,"-10.46%","-10.45%","-10.50%","-10.49%"
2003,"22.58%","22.57%","22.53%","22.52%"
2004,"20.03%","20.02%","19.98%","19.97%"
\end{filecontents*}
\documentclass[titlepage]{report}
\usepackage{graphicx}    
\usepackage{csvsimple} % Generates table from .csv

\newcommand{\csvloopx}[2][]{\csvloop{#1,#2}}
\newcommand{\csvautotabularx}[2][]{\csvloopx[#1]{autotabular={#2}}}
\newcommand{\respectpercent}{\catcode`\%=12\relax}

\usepackage{float}

\newcommand{\me}[1]{#1}

\begin{document}



\begin{table}[H]

    \begin{center}
    \me{\begingroup\catcode`"=9
     \csvreader[no head, tabular=|c|r|r|r|r|,%
        before table=\respectpercent,%
        table head=\hline \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Year}%
                 & \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Base}% 
                 & \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Round Lot \\ of 100 \\ Shares}% 
                 & \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per~Share}%  
                 & \multicolumn{1}{|p{2.5cm}|}{\centering \bfseries Transaction Cost \\ of \$0.01 \\ per Share and \\ Round Lot \\ of 100 Shares}\\\hline,% 
        late after line = \\\hline,%
            filter expr={
                test{\ifnumgreater{\thecsvinputline}{1}}}]%
        {\jobname.csv}%
        {1=\one, 2=\two, 3=\three, 4=\four, 5=\five}%
        {$\one$ & $\two$ & $\three$ & $\four$ & $\five$}%
    \endgroup}%
    \end{center}

    \caption{Market Cost Assumption}
    \label{ tab:Slippage}
\end{table}


\end{document}

This being said, I only fixed the % issue. Refer to @egreg's answer to the further needed improvements. I added package float to make the example compilable and copied-over filecontents* usage from @egreg's answer for the same reason.

As for the explanation it was given by @egreg. The % is set to not be a comment character when csvtable starts doing the table.

The earlier % were still comment characters when \csvreader fetched its arguments, hence behaved as such. With the \me method I am freezing the catcode of the last %, just to illustrate.

But much simpler is to use \relax in place of problematic % to gobble the end of line space (hmm... anyway the extra space token is removed by TeX when closing the paragraph, and your table is way too wide anyhow

Overfull \hbox (70.65941pt too wide) in paragraph at lines 40--42

)

The earlier ones are not needed as pointed out by @egreg. When a LaTeX command has N arguments given in brace pairs, intervening spaces are ignored... except if it is defined in some special way; further, csvreader probably ignores spaces after commas when parsing comma separated options.

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.