Text FormattingChoices Alignment in an Exam Sheet

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
ollydbg
Posts: 2
Joined: Thu Jan 31, 2013 12:06 pm

Choices Alignment in an Exam Sheet

Post by ollydbg »

Hi, I found some LaTeX code to automatically put the multiply choices in the questions, here is the code, I create a minimal sample test.

Code: Select all

\documentclass[a4paper]{article}
\usepackage{ifthen}
\usepackage{calc}

%usage \choice{ }{ }{ }{ }
%(A)(B)(C)(D)
\newcommand{\fourch}[4]{

	\begin{tabular}{*{4}{@{}p{0.23\textwidth}}}
        (A)~#1 & (B)~#2 & (C)~#3 & (D)~#4
	\end{tabular}
}

%(A)(B)
%(C)(D)
\newcommand{\twoch}[4]{

	\begin{tabular}{*{2}{@{}p{0.46\textwidth}}}
        (A)~#1 & (B)~#2
	\end{tabular}
	\par
	\begin{tabular}{*{2}{@{}p{0.46\textwidth}}}
        (C)~#3 & (D)~#4
	\end{tabular}
}

%(A)
%(B)
%(C)
%(D)
\newcommand{\onech}[4]{

	(A)~#1 \\ (B)~#2 \\ (C)~#3 \\ (D)~#4
}

\newlength\widthcha
\newlength\widthchb
\newlength\widthchc
\newlength\widthchd
\newlength\widthch
\newlength\tabmaxwidth

\setlength\tabmaxwidth{0.96\textwidth}
\newlength\fourthtabwidth
\setlength\fourthtabwidth{0.25\textwidth}
\newlength\halftabwidth
\setlength\halftabwidth{0.5\textwidth}

\newcommand{\choice}[4]{
	\settowidth\widthcha{AM.#1}\setlength{\widthch}{\widthcha}
	\settowidth\widthchb{BM.#2}	
	\ifthenelse{\widthch<\widthchb}{\setlength{\widthch}{\widthchb}}{}
	\settowidth\widthchb{CM.#3}	
	\ifthenelse{\widthch<\widthchb}{\setlength{\widthch}{\widthchb}}{}
	\settowidth\widthchb{DM.#4}	
	\ifthenelse{\widthch<\widthchb}{\setlength{\widthch}{\widthchb}}{}	
	\ifthenelse{\widthch<\fourthtabwidth}{\fourch{#1}{#2}{#3}{#4}}
			   {\ifthenelse{\widthch<\halftabwidth\and\widthch>\fourthtabwidth}{\twoch{#1}{#2}{#3}{#4}}
			   {\onech{#1}{#2}{#3}{#4}}}
}
\begin{document}

Question:
\choice{1}{2}{3}{4}
\par
Another Question:
\choice{hello world, hello wo}{hello world, h}{hello world, hello wd}{hello world, hello world}
\par
Third Question:
\choice{hello world, hello hello world,hello world,hello world,hello world,hello world,hello world,hello world,hello world,hello world,hello world,hello world,}{hello world, h}{hello world, hello wd}{hello world, hello world}

\end{document}
See the screen shot:
2013-01-31 21 17 31.png
2013-01-31 21 17 31.png (19.39 KiB) Viewed 13285 times
The questions are:
  1. Why does the third question's choice A is indent? But all the other choices were aligned correctly, any way to fix this?
  2. What does AM BM CM DM means?
Thanks.

Recommended reading 2024:

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

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

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Choices Alignment in an Exam Sheet

Post by localghost »

Perhaps it's easier to use a special class like exam to format your exam sheet.

Code: Select all

\documentclass[11pt]{exam}
\usepackage[T1]{fontenc}
\usepackage{multicol}

\renewcommand*{\choicelabel}{(\thechoice)}

\begin{document}
  \begin{questions}
    \question First Question

    \begin{oneparchoices}
      \choice 1
      \choice 2
      \choice 3
      \choice 4
    \end{oneparchoices}

    \question Another Question

    \begin{multicols}{2}
      \begin{choices}
        \choice hello world, hello wo
        \choice hello world, h
        \choice hello world, hello wd
        \choice hello world, hello world
      \end{choices}
    \end{multicols}

    \question Another Question

    \begin{choices}
      \choice The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
      \choice The quick brown fox jumps over the lazy dog.
      \choice The quick brown fox jumps over the lazy dog.
      \choice The quick brown fox jumps over the lazy dog.
    \end{choices}
  \end{questions}

\end{document}
For details regarding its capabilities and for further customization please refer to the class guide.


Alternatives:

Best regards and welcome to the board
Thorsten
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Choices Alignment in an Exam Sheet

Post by cgnieder »

Hi ollydbg,

Welcome to the LaTeX community.

Thorsten already mentioned the exsheets package. Here's an example of what you could do:

Code: Select all

\documentclass{article}
\usepackage{exsheets}
\SetupExSheets{
  question/type=exam ,
  headings=plain
}

% The following declares an empty question heading. If you don't
% want that leave the code alone and load one of the other heading
% styles provided by exsheets in the options above.
\DeclareInstance{exsheets-heading}{plain}{default}
  {
    runin = true ,
    number-post-code = \space ,
    attach = { main[l,vc]points[l,vc](\linewidth+\marginparsep,0pt) }
}

% The following declares an environment `choices' similar to list but
% instead of \item you use \choice
\NewTasks[
  counter-format = (tsk[A]) ,
  label-width = 2em
]{choices}[\choice]

\begin{document}

\begin{question}
 Question:
 % The number in parentheses declares the number of columns that
 % is used by the `choices' environment:
 \begin{choices}(4)
  \choice 1
  \choice 2
  \choice 3
  \choice 4
 \end{choices}
\end{question}

\begin{question}
 Another Question:
 \begin{choices}(2)
   \choice hello world, hello wo
   \choice hello world, h
   \choice hello world, hello wd
   \choice hello world, hello world
 \end{choices}
\end{question}

\begin{question}
 Third Question:
 \begin{choices}
  \choice hello world, hello hello world,hello world,hello world, hello world,
    hello world, hello world, hello world, hello world, hello world, hello world,
    hello world,
  \choice hello world, h
  \choice hello world, hello wd
  \choice hello world, hello world
 \end{choices}
\end{question}

\end{document}
choices.png
choices.png (24.69 KiB) Viewed 13268 times
Regards
Last edited by cgnieder on Sun Sep 15, 2013 2:12 pm, edited 1 time in total.
site moderator & package author
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Choices Alignment in an Exam Sheet

Post by localghost »

cgnieder wrote:[…] the exsheets package. Here's an example […]
I definitely have to take a closer at that package.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Choices Alignment in an Exam Sheet

Post by cgnieder »

To answer the original question: what you're seeing is the parindent. Every line except the last three is indented by it. Add \setlength\parindent{0pt} to your preamble to see that I'm right.

You might not have expected that but the macros \onech, \twoch and \fourch issue a \par at the beginning by having an empty line in them. I guess that is deliberate but I don't think it's good style. Let's have a look:

Code: Select all

\newcommand{\onech}[4]{

        (A)~#1 \\ (B)~#2 \\ (C)~#3 \\ (D)~#4
}
The line

Code: Select all

\newcommand{\onech}[4]{
is ended by an end-of-line character which normally is treated like a space. But since an empty line follows they are together treated as if a \par had been inserted. If this is deliberate I'd favor something like this:

Code: Select all

\newcommand{\onech}[4]{%
  \par
    (A)~#1 \\
    (B)~#2 \\
    (C)~#3 \\
    (D)~#4
  \par
}
that is issue the par explicitly. It's probably also a good idea to end the command with a \par, just in case. (writing everything in different lines is just my personal preference, there's no need for it).

Regards
site moderator & package author
ollydbg
Posts: 2
Joined: Thu Jan 31, 2013 12:06 pm

Choices Alignment in an Exam Sheet

Post by ollydbg »

Hi, thanks for the reply and help.
I see the exsheets package, but as the code comment said:

Code: Select all

\begin{question}
 Question:
 % The number in parentheses declares the number of columns that
 % is used by the `choices' environment:
 \begin{choices}(4)
  \choice 1
  \choice 2
  \choice 3
  \choice 4
 \end{choices}
\end{question}
I need to specify the column number of the choices.
What I want is the let the Latex engine to automatically put the choices in a compact way, whether it need one column, two or four columns. I think it is much better than manually specify the column numbers, right? I don't see any packages have such feature.

@cgnieder, thanks for the explanation of the "indent" and "an empty line" issue, I understand now.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Choices Alignment in an Exam Sheet

Post by cgnieder »

ollydbg wrote:What I want is the let the Latex engine to automatically put the choices in a compact way, whether it need one column, two or four columns. I think it is much better than manually specify the column numbers, right? I don't see any packages have such feature.
At least the »exsheets package cannot do that automatically. You can still stick to your original code. Now that you know the cause for the unwanted output you can take measures against it. :)

Regards
site moderator & package author
Post Reply