Text FormattingAlignment over two lists

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Alignment over two lists

Post by svend_tveskaeg »

HI all.

Consider the following MWE:

Code: Select all

\documentclass[danish]{article}
\usepackage{babel,enumitem}

\newcounter{animals}
\newenvironment{exercise}
  {\begin{list}{Answer~(\arabic{animals}):}%
               {\setlength{\labelsep}{8pt}%
                \setlength{\itemindent}{8pt}%
                \setlength{\leftmargin}{0pt}%
                \setlength{\labelwidth}{0pt}%
                \usecounter{animals}}}%
  {\end{list}}

\begin{document}

Wierd animal features:
\begin{enumerate}[label=Question~(\arabic*),leftmargin=*,labelsep=\parindent]
  \item Why is a horse not blue?
  \item Why can a goat not fly?
\end{enumerate}
Explanations:
\begin{exercise}
  \item It would look stupid.
  \item I actually have seen a flying goat.
\end{exercise}

\end{document}
I would like to automatically align ``Why is...''/``Why can...'' with ``It would...''/``I have...'' vertically. How do I do this?

Thank you in advance!

P.S. I can of course do it manually by changing the length of \labelsep and \itemindent.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Alignment over two lists

Post by cgnieder »

Why don't you simply define two new list for this with appropriate settings?

Code: Select all

\documentclass[danish]{article}
\usepackage{babel,enumitem}

\newlist{Questions}{enumerate}{1}
\setlist[Questions]{label=Question~(\arabic*),leftmargin=*,itemindent=4.5em,labelsep=*}

\newlist{Answers}{enumerate}{1}
\setlist[Answers]{label=Answer~(\arabic*):,leftmargin=*,itemindent=4.5em,labelsep=*}
\begin{document}

Wierd animal features:
\begin{Questions}
  \item Why is a horse not blue?
  \item Why can a goat not fly?
\end{Questions}
Explanations:
\begin{Answers}
  \item It would look stupid.
  \item I actually have seen a flying goat.
\end{Answers}

\end{document}
Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Alignment over two lists

Post by svend_tveskaeg »

Almost perfect.

If the answer (or question) is longer than the width of a line, I get an indent from line two onwards. How do I get rid of this indentation?

Thank you in advance!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Re: Alignment over two lists

Post by cgnieder »

I'm not sure I understand… do you mean you want the second line to align with the label, i.e. for example »Question (1)«?
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Alignment over two lists

Post by cgnieder »

The enumitem is a very good package, unfortunately I can't say the same about its manual. So maybe it is possible with it but I wasn't able to figure it out.

Here is a solution from scratch:

Code: Select all

\documentclass[danish]{article}
\usepackage{babel,enumitem}
\usepackage{lipsum}% for dummy text


\newlength\mylabelwidth
\setlength\mylabelwidth{7em}

\makeatletter
\newlength\my@labelindent
\setlength\my@labelindent{0pt}
\newcommand*\mylabelindent{\noindent\hspace*{\my@labelindent}}
\makeatother

\newcounter{question}
\newenvironment{questions}
  {%
    \def\Item{%
      \stepcounter{question}%
      \par\mylabelindent
      \parbox{\mylabelwidth}{Question~(\arabic{question}):}}%
    \vskip0pt\relax\addvspace{\baselineskip}% skip before the env
  }%
  {\par\noindent\vskip0pt\relax\addvspace{\baselineskip}}% skip after the env

\newcounter{answer}
\newenvironment{answers}
  {%
    \def\Item{%
      \stepcounter{answer}%
      \par\mylabelindent
      \parbox{\mylabelwidth}{Answer~(\arabic{answer}):}}%
    \vskip0pt\relax\addvspace{\baselineskip}%
  }%
  {\par\noindent\vskip0pt\relax\addvspace{\baselineskip}}

\begin{document}

Wierd animal features:
\begin{questions}
  \Item Why is a horse not blue? \lipsum[4]
  \Item Why can a goat not fly?
\end{questions}
Explanations:
\begin{answers}
  \Item It would look stupid.
  \Item I actually have seen a flying goat.
\end{answers}

\end{document}
Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Alignment over two lists

Post by svend_tveskaeg »

I am sure it is exactly what I am looking for, but I cannot compile your code. :( (The log file is attached.)

Is the problem my LaTeX distribution (TeX Live 2012; updated via tlmgr today) or is the code incomplete?
Attachments
test.log
Log file
(3.94 KiB) Downloaded 189 times
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Re: Alignment over two lists

Post by cgnieder »

Hm, that's strange... unfortunately the log does not say what's going on and simply ends with emergency stop. Have you tried deleting the aux file? (forget the last question if you created a new file)
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Alignment over two lists

Post by svend_tveskaeg »

Strange, indeed.

I have deleted the auxillery file (and tried with a new file).

A screenshot from when I try to compile the file (called test.tex) from a terminal is attached.

It simply freezes when it comes to the point shown in the screenshot.

Does it help?
Attachments
terminal.png
terminal.png (84.83 KiB) Viewed 5242 times
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Alignment over two lists

Post by cgnieder »

Not really. It does not give any new information. Your log file suggests that LaTeX reads at least until line 41. That line reads

Code: Select all

\Item Why is a horse not blue? \lipsum[4]
Maybe you can remove the \lipsum and see what happens (although I can't think of a reason why that shouldn't work. The code uses no esoteric packages or anything. Should be pretty working nicely (unless I'm missing something, obviously…)

Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Alignment over two lists

Post by svend_tveskaeg »

Nothing changes when the lipsum package (and the dummy text in the document itself) is removed.

I will try to figure out the problem and post an explanation if one comes to me.

Thank you for your help!

P.S. I will not be here for the next 12-16 hours, but if anyone can see the problem, please join the conversation. :)
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply