1

I want to make a command that can make the table a fixed width with text left vertically and top horizontal.

I want the table to look like this

enter image description here

But it looks like this

enter image description here

code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage[danish]{babel}
\usepackage{lscape}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color} 
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{float} 
\usepackage[top=2.81cm, bottom=2.75cm,right=2cm, left=2cm]{geometry}
\usepackage{url}
\usepackage[hidelinks]{hyperref}
\hypersetup{breaklinks=true}
\usepackage{setspace}
   \onehalfspacing
   \usepackage[T1]{fontenc}

\usepackage{etoolbox}
\usepackage{setspace}
\onehalfspacing
\usepackage{url}
\usepackage[hidelinks]{hyperref}
\hypersetup{breaklinks=true}
\newcounter{rowcntr}[table]
\makeatletter
\usepackage{longtable}
\newcolumntype{L}[2]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[2]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\renewcommand{\therowcntr}{\@arabic{\numexpr\c@table+1}.\arabic{rowcntr}}
\makeatother
\newcolumntype{N}{>{\refstepcounter{rowcntr}\therowcntr}c}
\AtBeginEnvironment{tabular}{\setcounter{rowcntr}{0}}
\begin{document}

\begin{center}
    \begin{longtable}{|N| l | L{7.6cm} | L{7.6cm} |}
    \hline
    \multicolumn{2}{|c}{} & \multicolumn{1}{c|}{\textbf{Question}} & \multicolumn{1}{c|}{\textbf{Why}} \\ \hline
   \multicolumn{4}{|c|}{Interview fase 1 - Indledende}\\ \hline
   \multicolumn{4}{|C{17.5cm}|}{\textit{Text TextTextTextTextText TextTextTextTextTextTextTextTextTextTextText TextText TextTextText TextTextTextTextTextTextTextTextTextText Text Text Text Text Text Text Text Text }}\\ \hline
   \label{que:1} & A & Why is it you think ...? & This is a good question.\\
   \label{que:2} & C & Why is it you think Text text Text textText textText text & This is a good question.\\
   \label{que:3} & E & Text text Text text Text text & Text text Text textText text Text textText text Text text Text text Text textText text Text text Text text.\\ \hline

   \caption {Questions} \label{tab:interviewspg}
    \end{longtable}
\end{center}
\end{document}
4
  • 1
    instead m column type use p type. Do you really need long table?
    – Zarko
    Mar 6, 2017 at 14:09
  • 2
    Three quick comments: (i) A longtable environment is centered automatically -- no need to embed it in a center environment. (ii) If you don't want to typeset something in italics, don't encase it in a \textit directive. (iii) Load the hyperref package last.
    – Mico
    Mar 6, 2017 at 14:10
  • Incidentally, are you aware that the width of the longtable exceeds \textwidth by a considerable amount?
    – Mico
    Mar 6, 2017 at 14:17
  • No what does that mean?
    – Jonas
    Mar 10, 2017 at 22:56

1 Answer 1

3

After removing all duplicate package and all, which are not relevant to your table and removing all surplus code from table... and considering my and Mico comments above I obtain the following table. Is it what you looking for?

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[showframe,% for this example only: show page layout
            top=2.81cm, bottom=2.75cm, 
            right=2cm, left=2cm]{geometry}
\usepackage{setspace}
   \onehalfspacing


\usepackage{etoolbox}
\usepackage{array,longtable}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{\dimexpr#1-2\tabcolsep-\arrayrulewidth}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{\dimexpr#1-2\tabcolsep-\arrayrulewidth}}
\newcounter{rowcntr}[table]
\renewcommand{\therowcntr}{\arabic{table}.\arabic{rowcntr}}
\newcolumntype{N}[1]{>{\refstepcounter{rowcntr}\therowcntr\centering}p{\dimexpr#1-2\tabcolsep-\arrayrulewidth}}
\AtBeginEnvironment{tabular}{\setcounter{rowcntr}{0}}

\begin{document}
some text
{    \sffamily
    \begin{longtable}{| N{0.05\linewidth} 
                      | C{0.05\linewidth} 
                      | L{0.45\linewidth} 
                      | L{0.45\linewidth} |}
    \hline
                &   &   \hfil\textbf{Question}      &   \hfil\textbf{Why}        \\ \hline
\multicolumn{4}{|c|}{Interview phase 1 - Indledende}    \\ \hline
\multicolumn{4}{|C{\linewidth}|}{Text TextTextTextTextText
                                TextTextTextTextTextTextTextTextTextTextText 
                                TextText TextTextText TextTextTextTextTextTextTextTextTextText 
                                Text Text Text Text Text 
                                Text Text Text}                                 \\ \hline
\label{que:1}   & A & Why is it you think ...\dots? & This is a good question.  \\
\label{que:2}   & C & Why is it you think Text text 
                     Text textText textText text    & This is a good question.  \\
\label{que:3}   & E & Text textText text Text text  & Text text Text textText 
                                                      text Text textText text Text text Text text Text textText text Text text Text text.           \\ \hline
   \caption{Questions} 
\label{tab:interviewspg}
    \end{longtable}
}
\end{document}
3
  • Very nice. My compliments. I vote your question.
    – Sebastiano
    Mar 6, 2017 at 15:00
  • Yes exactly! Thank you, what was wrong with the packages?
    – Jonas
    Mar 6, 2017 at 15:14
  • @Jonas, (i) for MWE is not necessary to have all your packages, (ii) some packages you load twice (setspace, hypperlink ... check your document). I update my answer -- remove some more spurious code remnants. Here is habit that instead "thank you" you vote answer or even accept it :-)
    – Zarko
    Mar 6, 2017 at 15:58

You must log in to answer this question.

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