2

I was trying to do an enumeration with columns, which references one item from another one in the same list. I figured out that the best way is to make a table with enumerated rows. I had a working solution, however next thing I tried to do was to add PDF references with hyperref package. However, this produces quite strange results.

My solution is three column table: first one is right-aligned for the number of the row, next one has some specified length and the third one uses all the remaining space, therefore tabularx is needed. A short code sample:

\usepackage{tabularx}
\usepackage{hyperref}
\newcounter{hdItemCounter}
\setcounter{hdItemCounter}{0}

\begin{document}

\begin{tabularx}{\textwidth}{@{}rp{50pt}X}
    \refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem1} & Some text to fill at least two lines. & Some text\\
    \refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem2} & Some text. & Some text\\
    \refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem3} & Some text. & Some text \ref{hdItem2} ir \ref{hdItem1}\\
    \refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem4} & Some text. & Some text\\
    \refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem5} & Some text. & Some text \ref{hdItem3} ir \ref{hdItem4} \\
\end{tabularx}
%Some text to fill at least several pages to see the strange effect with hyperref.
\end{document}

I need a lot of such tables, therefore, I created new environment and new \item-style command:

\newenvironment{someenvironment}[1]%
    {\tabularx{\textwidth}{@{}rp{#1}X}}
    {\endtabularx}
\newcommand{\someitem}[1]{\refstepcounter{hdItemCounter}\thehdItemCounter.\label{#1} &}

First I was trying to do this with listliketab, but I wasn't able to put it inside my new environment. However, even without the new environment hyperref acts strangely. All the references are produced, however they point to wrong places: top of the page is at the line bottom and not at the top:

enter image description here

This can be changed by changing the type of first column to p:

\begin{tabularx}{\textwidth}{@{}p{10pt}p{50pt}X}

But then again - I need right-aligned first column. Nevertheless, this produces even stranger results: now references point to the top of the number, but the number is placed not at the top of the row:

enter image description here

This problem can be solved by writing something in front of \refstepcounter:

I\refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem1}

By the way, \phantom{I} does not work, however forced space symbol does:

\ \refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem1}

I am sorry for the lengthy post, but I wanted to be as specific as I can. Can anyone, please, suggest a better solution to my problem? I need every pt of the page width in order to display formulas as clearly as possible, therefore it would be very nice to have the r column and avoid putting space in every line.

I will be really grateful for any help and comments.

1 Answer 1

1

To specify the alignment of a column of the type p{<width>} you can insert different declarations with the command >{decl.}.

To get a right align column with a specific width set >{\raggedleft}p{10pt}

To simplify the modification you can use the following table header:

\documentclass{article}
\usepackage{tabularx}
\usepackage{hyperref}
\newcounter{hdItemCounter}
\setcounter{hdItemCounter}{0}
\usepackage{showframe}
\begin{document}

\noindent\begin{tabularx}{\textwidth}%
  {@{\makebox[30pt][l]{\refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem\thehdItemCounter}}}p{50pt}X}
Some text to fill at least two lines. & Some text\\
Some text. & Some text\\
Some text. & Some text \ref{hdItem2} ir \ref{hdItem1}\\
Some text. & Some text\\
Some text. & Some text \ref{hdItem3} ir \ref{hdItem4} \\
\end{tabularx}
Some text to fill at least several pages to see the strange effect with hyperref.
\end{document}

This can be simplified by your own environment:

\documentclass{article}
\usepackage{tabularx}
\usepackage{hyperref}
\newcounter{hdItemCounter}
\setcounter{hdItemCounter}{0}
\usepackage{showframe}
\newenvironment{specifictable}{%
\tabularx\linewidth{@{\makebox[30pt][l]{\refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem\thehdItemCounter}}}p{50pt}X}}{\endtabularx}
\begin{document}

\noindent\begin{tabularx}{\textwidth}%
  {@{\makebox[30pt][l]{\refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem\thehdItemCounter}}}p{50pt}X}
Some text to fill at least two lines. & Some text\\
Some text. & Some text\\
Some text. & Some text \ref{hdItem2} ir \ref{hdItem1}\\
Some text. & Some text\\
Some text. & Some text \ref{hdItem3} ir \ref{hdItem4} \\
\end{tabularx}
Some text to fill at least several pages to see the strange effect with hyperref.

\noindent\begin{specifictable}
Some text to fill at least two lines. & Some text\\
Some text. & Some text\\
Some text. & Some text \ref{hdItem2} ir \ref{hdItem1}\\
Some text. & Some text\\
Some text. & Some text \ref{hdItem3} ir \ref{hdItem4} \\
\end{specifictable}
\end{document}

The code above produces: enter image description here

2
  • Thanks for your answer. However, it doesn't solve my problem. The solution with the box produces a correct looking table, however if you try to push on number 1 in the PDF, it directs you to the line "to fill at" instead of directing to the first line. If the start of the environment is changed to: \begin{tabularx}{\textwidth}{@{}>{\raggedleft}p{10pt}p{50pt}X}, then the table is corrupted. I will try and experiment with it more on Monday. Thanks for your effort.
    – Juliusan
    Sep 3, 2011 at 17:50
  • My solution was to use >{\raggedleft}p{...} and to start every line with \noindent: \begin{tabularx}{\textwidth}{|@{}>{\raggedleft}p{14pt}@{}|p{50pt}|X|} \noindent\refstepcounter{hdItemCounter}\thehdItemCounter.\label{hdItem1}. I don't know, why it works and why omitting \noindent brakes the table. It was just a result of trial and error. So thanks for you help.
    – Juliusan
    Sep 5, 2011 at 20:58

You must log in to answer this question.

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