1

I am using latex exam class for short questions which provide lines for answers. The following code codes gives me lines for answers which span the full width of the page. I want to control the width of these lines so that I can reduce them according to my choice (1 inch, 2in etc.)

\begin{document}
\begin{questions}
\question What is your name? \fillwithlines{3cm}
\end{questions}
\end{document}

As an example, this is what I get with the existing code:

enter image description here

Whereas I want something the following: enter image description here

I would appreciate for any help in this regard. Thanks

1
  • If you use multicols, paracol or a minipage, the lines will reduce to fit the column width. Commented Mar 14, 2023 at 19:18

2 Answers 2

1

As mentioned in the comments, using a minipage environment is probably the simplest solutions. If you're doing this a lot you can create a minipage version of the \fillwithlines command. This takes an optional width argument which defaults to \linewidth if not used.

\documentclass{exam}
\NewDocumentCommand{\Fillwithlines}{O{\linewidth}m}{
\par\begin{minipage}{#1}
\fillwithlines{#2}
\end{minipage}}
\begin{document}
\begin{questions}
\question What is the answer to this question?

\Fillwithlines[3in]{1in}

\question What is the answer to this question?

\Fillwithlines{1in}
\end{questions}
\end{document}

output of code

8
  • You didn't mention that in your question, and you didn't give enough of a sample document to test. I'm happy to modify this answer to account for the revised question, but please edit the question giving a compilable sample document like the one I gave in my answer that I can play with.
    – Alan Munn
    Commented Mar 16, 2023 at 16:26
  • Sorry Alan. I was writing my whole comment and accidently pressed entered which posted the comment. Here is the full comment: Thanks Alan. There is one issue that I am running into. Since I wanted the solution to print as well, so I had to use "\begin{solutionorlines}" environment as follows and I am wondering how to tweak your suggested solution to this case? Write first three aphabets. \begin{solutionorlines}[3cm] \begin{color}{red} \begin{enumerate} \item a \item b \item c \end{enumerate} \end{color} \end{solutionorlines}
    – Tash
    Commented Mar 16, 2023 at 16:29
  • @Tash As I said, edit your question, with a complete minimal document that can be used rather than giving code in the comment.
    – Alan Munn
    Commented Mar 16, 2023 at 16:40
  • OK. Will do that now. Sorry for the confusion.
    – Tash
    Commented Mar 16, 2023 at 16:44
  • Alan. Since your solution is the best for this question so we keep this thread as is (instead of confusing those who see this later, I think I should post my problem as a new question because as it is kind of different. What do you say?
    – Tash
    Commented Mar 16, 2023 at 16:59
0

This code replaces the hard-wired length of the lines (\hsize) with a custom length (\linelength) that can be set before a question.

b

\documentclass{exam}
    
%**************************************** added <<<<<<<<<<<<
\usepackage{xpatch}
\newlength{\linelength}
\setlength{\linelength}{\hsize}% default length
\makeatletter
\xpatchcmd{\do@fillwithlines}
{\hsize}{\linelength}{}{}
\makeatother
%****************************************

\begin{document}
    \begin{questions}
        \setlength{\linelength}{1.5in}% choose the line length <<<<<<<<<<<<<<<<<<<<
        \question What is your name? \fillwithlines{3cm}
                    
        \setlength{\linelength}{2.5in}% choose the line length <<<<<<<<<<<<<<<<<<<<
        \question What is your name? \fillwithlines{3cm}
    \end{questions}

\end{document}

You must log in to answer this question.

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