Search code examples
importlatextabularpdflatex

\hline issue with LaTex when importing table from different .tex file into a tabular environment


I'm having an issue where at certain points using the \hline function creates an error and instead displays the word "height" where there should be a line. This is happening only when I use an /hline immediately after using a \input{".../filename.tex"} command in a tabular environment. Interestingly, though, adding an \hline to the last row of the file I am importing produces an \hline properly. Does anyone have an idea why this may be the case and what I can do to fix it so that I can add the \hline to the main file and not the files I'm importing. Note that I use TexMaker and MiKTeX

Here are some examples of my code that doesn't work:

\documentclass{12pt, english}{article}
\usepackage{geometry}
\usepackage{caption}
\usepackage{tocvsec2}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{subfloat}
\usepackage{subcaption}
\usepackage{enumitem}
\usepackage{tabularx}
\usepackage{tabulary}
\usepackage{tabu}
\usepackage{titlesec}
\usepackage{tocloft}
\begin{document} 

\begin{table}
\caption{Caption} \label{tab:label} \centering
\begin{tabular}{lccc}
\hline
   \import{".../filename.tex"}
\hline
\end{tabular}
\begin{minipage}{0.95}
Notes are here
\end{minipage}
\end{table}

\end{document}

where filename.tex looks like this (with nothing else):

& Mean & SD & N \\
\midrule
Var 1 & 1 & 0 & 1000 \\
Var 2 & 1 & 0 & 1000 \\
Var 3 & 1 & 0 & 1000 \\
Var 4 & 1 & 0 & 1000 \\
Var 5 & 1 & 0 & 1000 \\

When I move the last \hline to the end of filname.tex, however, it works fine.


Solution

  • You can use the same trick as in https://tex.stackexchange.com/a/50700/36296

    Other problems:

    • the syntax \documentclass{12pt, english}{article} is wrong

    • the booktabs package is missing

    • missing unit in \begin{minipage}{0.95}

    • you really, really, really shouldn't use the tabu package, it is more or less completely broken

    • don't load the same package multiple times

    \documentclass[12pt, english]{article}
    \usepackage{geometry}
    \usepackage{caption}
    \usepackage{tocvsec2}
    \usepackage{graphicx}
    \usepackage{multirow}
    %\usepackage{tabularx}
    \usepackage{subfloat}
    \usepackage{subcaption}
    \usepackage{enumitem}
    \usepackage{tabularx}
    \usepackage{tabulary}
    %\usepackage{tabu}
    \usepackage{titlesec}
    \usepackage{tocloft}
    \usepackage{booktabs}
    
    \begin{filecontents*}[overwrite]{test.tex}
    & Mean & SD & N \\
    \midrule
    Var 1 & 1 & 0 & 1000 \\
    Var 2 & 1 & 0 & 1000 \\
    Var 3 & 1 & 0 & 1000 \\
    Var 4 & 1 & 0 & 1000 \\
    Var 5 & 1 & 0 & 1000 \\
    \end{filecontents*}
    
    \makeatletter
    \newcommand\primitiveinput[1]
    {\@@input #1 }
    \makeatother
    
    
    \begin{document} 
    
    \begin{table}
    \caption{Caption} \label{tab:label} \centering
    \begin{tabular}{lccc}
    \hline
       \primitiveinput{test}
    \hline
    \end{tabular}
    \begin{minipage}{0.95cm}
    Notes are here
    \end{minipage}
    \end{table}
    
    \end{document}