Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to generate a table containing some static text and some text that should be supplied from a defined macro. Consider the following MWE:

\documentclass[a4paper,12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[norsk]{babel}
\usepackage{fouriernc, graphicx, booktabs, multirow, tabularx}
\usepackage[showframe,width=0.85\paperwidth]{geometry}

\newcommand{\lsubject}{Naturfag}
\newcommand{\ldate}{20.08.13}
\newcommand{\lclass}{9D}
\newcommand{\lduration}{90 min.}
\newcommand{\lstart}{0815}
\newcommand{\lend}{0945}
\newcommand{\lcompetence}{Gjøre greie for hvilke biotiske og abiotiske faktorer som inngår i et økosystem og forklare sammenhengen mellom faktorene.}

\begin{document}
\noindent
\begin{tabularx}{1\textwidth}{*{4}{X}}
    \toprule
    Dato: \ldate & Klasse: \lclass & Varighet: \lduration & Tidspunkt: \lstart--\lend \\
    Fag: \lsubject & \multicolumn{3}{l}{Kompetansemål (K06): \lcompetence} \\
    \bottomrule
\end{tabularx}
\end{document}

With the flattering output of

enter image description here

The intention is that the text in \lcompetence should fill 3/4 of the page (such that the columns in the first row is evenly distributed across the text width, thus the multicolumn environment in row 2), but it doesn't linebreak properly. It rather squeezes the other columns together. I've tried to wrap the \lcompetence in a parbox, but that resulted in the lower left cell Fag: Naturfag (it's not English :p) being centered with respect to height in the cell rather than in the top of it.

I've tried some solutions with both the tabular and the tabularx environment, thus the question title.

Is there an elegant way of fixing this so the \lcompetence content fills 3/4 of the text width nicely? It feels like this question could have been answered before, but I'm not able to find an answer.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You have to teach tabularx to use the correct horizontal size.

\documentclass[a4paper,12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[norsk]{babel}
\usepackage{fouriernc, graphicx, booktabs,tabularx}
\usepackage[showframe,width=0.85\paperwidth]{geometry}

\newcommand{\lsubject}{Naturfag}
\newcommand{\ldate}{20.08.13}
\newcommand{\lclass}{9D}
\newcommand{\lduration}{90 min.}
\newcommand{\lstart}{0815}
\newcommand{\lend}{0945}

\newcommand{\lcompetence}{Gjøre greie for hvilke biotiske og abiotiske faktorer som inngår 
i et økosystem og forklare sammenhengen mellom faktorene.}

\begin{document}
\noindent
\begin{tabularx}{1\textwidth}{*{4}{X}}
\toprule
Dato: \ldate & Klasse: \lclass & Varighet: \lduration & Tidspunkt: \lstart--\lend \\
Fag: \lsubject & 
  \multicolumn{3}{>{\hsize=\dimexpr 3\hsize+4\tabcolsep\relax}X}
    {Kompetansemål (K06): \lcompetence} \\
\bottomrule
\end{tabularx}
\end{document}

But there's no need to use tabularx for this application:

\documentclass[a4paper,12pt]{article}

\usepackage[utf8]{inputenc}
\usepackage[norsk]{babel}
\usepackage{fouriernc, graphicx, booktabs,tabularx}
\usepackage[showframe,width=0.85\paperwidth]{geometry}

\newcommand{\lsubject}{Naturfag}
\newcommand{\ldate}{20.08.13}
\newcommand{\lclass}{9D}
\newcommand{\lduration}{90 min.}
\newcommand{\lstart}{0815}
\newcommand{\lend}{0945}

\newcommand{\lcompetence}{Gjøre greie for hvilke biotiske og abiotiske faktorer som inngår 
i et økosystem og forklare sammenhengen mellom faktorene.}

\begin{document}
\noindent
\begin{tabular}{*{4}{p{\dimexpr.25\textwidth-2\tabcolsep}}}
\toprule
Dato: \ldate & Klasse: \lclass & Varighet: \lduration & Tidspunkt: \lstart--\lend \\
Fag: \lsubject & 
  \multicolumn{3}{p{\dimexpr .75\textwidth-2\tabcolsep\relax}}
    {Kompetansemål (K06): \lcompetence} \\
\bottomrule
\end{tabular}
\end{document}

enter image description here

share|improve this answer
1  
+1 for saying not to use my package (although I really ought to make multicolumn easier in TX....) –  David Carlisle Aug 19 '13 at 16:09
    
Nice! Thanks for both explaining what I didn't understand at all with the multicolumn column width, and supplying a smarter solution. Takktakk =) –  Holene Aug 19 '13 at 16:14

Use \multicolumn{3}{p{.75\linewidth}}{Kompetansemål (K06): \lcompetence} (since you're using exactly 4 X-columns). The left-alignment doesn't allow for line-breaking.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.