LaTeX forum ⇒ Text FormattingChoices Alignment in an Exam Sheet

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

Choices Alignment in an Exam Sheet

Postby ollydbg » Thu Jan 31, 2013 3:19 pm

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.
  1. \documentclass[a4paper]{article}
  2. \usepackage{ifthen}
  3. \usepackage{calc}
  4.  
  5. %usage \choice{ }{ }{ }{ }
  6. %(A)(B)(C)(D)
  7. \newcommand{\fourch}[4]{
  8.  
  9. \begin{tabular}{*{4}{@{}p{0.23\textwidth}}}
  10. (A)~#1 & (B)~#2 & (C)~#3 & (D)~#4
  11. \end{tabular}
  12. }
  13.  
  14. %(A)(B)
  15. %(C)(D)
  16. \newcommand{\twoch}[4]{
  17.  
  18. \begin{tabular}{*{2}{@{}p{0.46\textwidth}}}
  19. (A)~#1 & (B)~#2
  20. \end{tabular}
  21. \par
  22. \begin{tabular}{*{2}{@{}p{0.46\textwidth}}}
  23. (C)~#3 & (D)~#4
  24. \end{tabular}
  25. }
  26.  
  27. %(A)
  28. %(B)
  29. %(C)
  30. %(D)
  31. \newcommand{\onech}[4]{
  32.  
  33. (A)~#1 \\ (B)~#2 \\ (C)~#3 \\ (D)~#4
  34. }
  35.  
  36. \newlength\widthcha
  37. \newlength\widthchb
  38. \newlength\widthchc
  39. \newlength\widthchd
  40. \newlength\widthch
  41. \newlength\tabmaxwidth
  42.  
  43. \setlength\tabmaxwidth{0.96\textwidth}
  44. \newlength\fourthtabwidth
  45. \setlength\fourthtabwidth{0.25\textwidth}
  46. \newlength\halftabwidth
  47. \setlength\halftabwidth{0.5\textwidth}
  48.  
  49. \newcommand{\choice}[4]{
  50. \settowidth\widthcha{AM.#1}\setlength{\widthch}{\widthcha}
  51. \settowidth\widthchb{BM.#2}
  52. \ifthenelse{\widthch<\widthchb}{\setlength{\widthch}{\widthchb}}{}
  53. \settowidth\widthchb{CM.#3}
  54. \ifthenelse{\widthch<\widthchb}{\setlength{\widthch}{\widthchb}}{}
  55. \settowidth\widthchb{DM.#4}
  56. \ifthenelse{\widthch<\widthchb}{\setlength{\widthch}{\widthchb}}{}
  57. \ifthenelse{\widthch<\fourthtabwidth}{\fourch{#1}{#2}{#3}{#4}}
  58. {\ifthenelse{\widthch<\halftabwidth\and\widthch>\fourthtabwidth}{\twoch{#1}{#2}{#3}{#4}}
  59. {\onech{#1}{#2}{#3}{#4}}}
  60. }
  61. \begin{document}
  62.  
  63. Question:
  64. \choice{1}{2}{3}{4}
  65. \par
  66. Another Question:
  67. \choice{hello world, hello wo}{hello world, h}{hello world, hello wd}{hello world, hello world}
  68. \par
  69. Third Question:
  70. \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}
  71.  
  72. \end{document}


See the screen shot:
2013-01-31 21 17 31.png
2013-01-31 21 17 31.png (19.39 KiB) Viewed 7398 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.

Tags:

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Thu Jan 31, 2013 6:41 pm

Perhaps it's easier to use a special class like exam to format your exam sheet.
  1. \documentclass[11pt]{exam}
  2. \usepackage[T1]{fontenc}
  3. \usepackage{multicol}
  4.  
  5. \renewcommand*{\choicelabel}{(\thechoice)}
  6.  
  7. \begin{document}
  8. \begin{questions}
  9. \question First Question
  10.  
  11. \begin{oneparchoices}
  12. \choice 1
  13. \choice 2
  14. \choice 3
  15. \choice 4
  16. \end{oneparchoices}
  17.  
  18. \question Another Question
  19.  
  20. \begin{multicols}{2}
  21. \begin{choices}
  22. \choice hello world, hello wo
  23. \choice hello world, h
  24. \choice hello world, hello wd
  25. \choice hello world, hello world
  26. \end{choices}
  27. \end{multicols}
  28.  
  29. \question Another Question
  30.  
  31. \begin{choices}
  32. \choice The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
  33. \choice The quick brown fox jumps over the lazy dog.
  34. \choice The quick brown fox jumps over the lazy dog.
  35. \choice The quick brown fox jumps over the lazy dog.
  36. \end{choices}
  37. \end{questions}
  38.  
  39. \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
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

User avatar
cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Thu Jan 31, 2013 7:58 pm

Hi ollydbg,

Welcome to the LaTeX community.

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

  1. \documentclass{article}
  2. \usepackage{exsheets}
  3. \SetupExSheets{
  4. question/type=exam ,
  5. headings=plain
  6. }
  7.  
  8. % The following declares an empty question heading. If you don't
  9. % want that leave the code alone and load one of the other heading
  10. % styles provided by exsheets in the options above.
  11. \DeclareInstance{exsheets-heading}{plain}{default}
  12. {
  13. runin = true ,
  14. number-post-code = \space ,
  15. attach = { main[l,vc]points[l,vc](\linewidth+\marginparsep,0pt) }
  16. }
  17.  
  18. % The following declares an environment `choices' similar to list but
  19. % instead of \item you use \choice
  20. \NewTasks[
  21. counter-format = (tsk[A]) ,
  22. label-width = 2em
  23. ]{choices}[\choice]
  24.  
  25. \begin{document}
  26.  
  27. \begin{question}
  28. Question:
  29. % The number in parentheses declares the number of columns that
  30. % is used by the `choices' environment:
  31. \begin{choices}(4)
  32. \choice 1
  33. \choice 2
  34. \choice 3
  35. \choice 4
  36. \end{choices}
  37. \end{question}
  38.  
  39. \begin{question}
  40. Another Question:
  41. \begin{choices}(2)
  42. \choice hello world, hello wo
  43. \choice hello world, h
  44. \choice hello world, hello wd
  45. \choice hello world, hello world
  46. \end{choices}
  47. \end{question}
  48.  
  49. \begin{question}
  50. Third Question:
  51. \begin{choices}
  52. \choice hello world, hello hello world,hello world,hello world, hello world,
  53. hello world, hello world, hello world, hello world, hello world, hello world,
  54. hello world,
  55. \choice hello world, h
  56. \choice hello world, hello wd
  57. \choice hello world, hello world
  58. \end{choices}
  59. \end{question}
  60.  
  61. \end{document}


choices.png
choices.png (24.69 KiB) Viewed 7381 times


Regards
Last edited by cgnieder on Sun Sep 15, 2013 2:12 pm, edited 1 time in total.
Reason: updated according to syntax changes in exsheets
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Thu Jan 31, 2013 8:58 pm

cgnieder wrote:[…] the exsheets package. Here's an example […]

I definitely have to take a closer at that package.
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

User avatar
cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Thu Jan 31, 2013 10:26 pm

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:

  1. \newcommand{\onech}[4]{
  2.  
  3. (A)~#1 \\ (B)~#2 \\ (C)~#3 \\ (D)~#4
  4. }


The line
  1. \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:

  1. \newcommand{\onech}[4]{%
  2. \par
  3. (A)~#1 \\
  4. (B)~#2 \\
  5. (C)~#3 \\
  6. (D)~#4
  7. \par
  8. }

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
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

ollydbg
Posts: 2
Joined: Thu Jan 31, 2013 12:06 pm

Postby ollydbg » Fri Feb 01, 2013 5:08 am

Hi, thanks for the reply and help.
I see the exsheets package, but as the code comment said:
  1. \begin{question}
  2. Question:
  3. % The number in parentheses declares the number of columns that
  4. % is used by the `choices' environment:
  5. \begin{choices}(4)
  6. \choice 1
  7. \choice 2
  8. \choice 3
  9. \choice 4
  10. \end{choices}
  11. \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: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Fri Feb 01, 2013 12:17 pm

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
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim


Return to “Text Formatting”

Who is online

Users browsing this forum: Google [Bot] and 4 guests