6

Preface: I am very new to Latex, and may have missed some things here.

I have a custom macro to create a horizontal line - not by my making, simply copied and pasted from the exam package.

\newlength\linefillheight
\newlength\linefillthickness
\setlength\linefillheight{.25in}
\setlength\linefillthickness{0.1pt}

\newcommand\linefill{\leavevmode
    \leaders\hrule height \linefillthickness \hfill\kern\z@}

Unfortunately I have discovered that it doesn't work inside a tabbing environment:

\begin{document}
\linefill
\begin{tabbing}
\linefill

The first \linefill generates a line, but the second does not. How can I change my Latex to have this work with the tabbing environment?

Here is an example .tex file that compiles and doesn't produce any horizontal lines, even though I want it to: (also note that \usepackage{examlines} refers to a custom .sty file I made that houses the code from the exam package for \linefill)

\documentclass[a4paper, 12pt]{article}
\usepackage{mathptmx}
\usepackage{examlines}
\newcommand{\tab}{\hspace*{1em}}

\begin{document}
\begin{tabbing}

\textbf{Question 1} \\ 
\textbf{a.} \tab \= Let $y = \left(- 3 x^{2} - 3 x\right)^{3}$. Find $\frac{dy}{dx}$. \\ 
\\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 
\textbf{b.} \tab Let $f(x) = e^{2 x^{2} + 9 x + 5}$. Evaluate $f'(-1)$. \\ 
\\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 


Let $f(x) = \left(- 3 x^{2} - 3 x\right)^{3} = u^{3}, u = - 3 x^{2} - 3 x$ \\ 
$f'(x) = 3 u^{2} \times u'$ \\ 
$f'(x) = - 81 x^{2} \left(x + 1\right)^{2} \left(2 x + 1\right)$ \\ 
\\
$f'(x) = \left(4 x + 9\right) e^{2 x^{2} + 9 x + 5}$ \\ 
$f'(-1) = \frac{5}{e^{2}}$ \\ 

\end{tabbing}
\end{document}

Here is the code in examlines.sty: (I don't use all of it)

%--------------------------------------------------------------------
%                            \fillwithlines


% \fillwithlines takes one argument, which is either a length or \fill
% or \stretch{number}, and it fills that much vertical space with
% horizontal lines that run the length of the current line.  That is,
% they extend from the current left margin (which depends on whether
% we're in a question, part, subpart, or subsubpart) to the right
% margin.
%
% The distance between the lines is \linefillheight, whose default value
% is set with the command
%
% \setlength\linefillheight{.25in}
%
% This value can be changed by giving a new \setlength command.
%
% The thickness of the lines is \linefillthickness, whose default value
% is set with the command
%
% \setlength\linefillthickness{.1pt}
%
% This value can be changed by giving a new \setlength command.


\newlength\linefillheight
\newlength\linefillthickness
\setlength\linefillheight{.25in}
\setlength\linefillthickness{0.1pt}

\newcommand\linefill{\leavevmode
    \leaders\hrule height \linefillthickness \hfill\kern\z@}


\def\fillwithlines#1{%
  \begingroup
  \ifhmode
    \par
  \fi
  \hrule height \z@
  \nobreak
  \setbox0=\hbox to \hsize{\hskip \@totalleftmargin
          \vrule height \linefillheight depth \z@ width \z@
          \linefill}%
  % We use \cleaders (rather than \leaders) so that a given
  % vertical space will always produce the same number of lines
  % no matter where on the page it happens to start:
  \cleaders \copy0 \vskip #1 \hbox{}%
  \endgroup
}
%--------------------------------------------------------------------

\newcommand{\e}{\mathrm{e}}
  • 1
    The definition of your \linefill command is wrong. I don't know how to fix it, but it certainly shouldn't have the word "height" in the middle of it like that. If you just want a horizontal line across the page, perhaps this answer will help: tex.stackexchange.com/a/19582/25356. If not, then perhaps you could add to your post the contents of a small compilable .tex file, starting with \documentclass and ending with \end{document}, that illustrates your problem? Then I can paste it into my editor, reproduce your situation on my machine, and start thinking about a solution. – John Wickerson Jun 10 '13 at 5:08
  • @JohnWickerson that sounds like a good idea :- ). I have now edited the question to include a compilable .tex file. Note that the line \usepackage{examlines} refers to a .sty file I made that houses the programming for \linefill – nebffa Jun 10 '13 at 6:00
  • Could you include the contents of the examlines package too? Or at least the definition of \linefill. – John Wickerson Jun 10 '13 at 6:36
  • @JohnWickerson Done! I've added the whole contents of examlines – nebffa Jun 10 '13 at 7:22
  • @JohnWickerson The syntax \hrule height <dimension> is perfectly good. – egreg Jun 10 '13 at 9:16
5
+50

I've used LaTeX for over 25 years and I don't think I've ever really seen a good use for tabbing but anyway I think that you want lines to look like this?

enter image description here

\documentclass[a4paper, 12pt]{article}
\usepackage{mathptmx}

\newcommand{\tab}{\hspace*{1em}}
\makeatletter
\def\linefill{%
\leavevmode
\leaders\hrule\hskip\dimexpr\textwidth -\@tempdima\mbox{}}
\begin{document}
\begin{tabbing}

\textbf{Question 1} \\ 
\textbf{a.} \tab \= Let $y = \left(- 3 x^{2} - 3 x\right)^{3}$. Find $\frac{dy}{dx}$. \\ 
\\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 
\textbf{b.} \tab Let $f(x) = e^{2 x^{2} + 9 x + 5}$. Evaluate $f'(-1)$. \\ 
\\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 
\> \linefill \\ 


Let $f(x) = \left(- 3 x^{2} - 3 x\right)^{3} = u^{3}, u = - 3 x^{2} - 3 x$ \\ 
$f'(x) = 3 u^{2} \times u'$ \\ 
$f'(x) = - 81 x^{2} \left(x + 1\right)^{2} \left(2 x + 1\right)$ \\ 
\\
$f'(x) = \left(4 x + 9\right) e^{2 x^{2} + 9 x + 5}$ \\ 
$f'(-1) = \frac{5}{e^{2}}$ \\ 

\end{tabbing}

\noindent X\dotfill X
\end{document}
  • That's exactly how I want the lines to look!!! The only question I have is - is there a way to modify the \linefill so that I can modify how much whitespace is produced after each line? – nebffa Jun 16 '13 at 7:56
  • I could add in some \vspace after each \linefill, which is not too much of a problem because I am writing a Python script to generate the latex, but that seems a bit hackish and so I'd like to not do that if possible :- ) – nebffa Jun 16 '13 at 9:37
  • 1
    @nebffa don't modify \linefill just use \\[2cm] instead of \\ – David Carlisle Jun 16 '13 at 9:38
  • Hey David - I really liked your solution, it was really simple to a newbie like me and it's the one I'm going with. Thanks so much!!! – nebffa Jun 19 '13 at 7:27
5

I like David's answer, but I couldn't let go the opportunity to use \tikzmark. In this, \tikzmark is used to place a mark at the desired x-position where the lines should begin, and then \DrawLines is used to draw the lines; the syntax is

\DrawLines[<length>]{<number>}{<mark>}

where <number> is a positive integer representing the amount of lines to be drawn, and <mark> is a previously stablished mark set with \tikzmark; <length> allows control over the vertical separation between consecutive lines.

The code:

\documentclass[a4paper, 12pt]{article}
\usepackage{mathptmx}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}

\newcommand{\tab}{\hspace*{1em}}

\newcounter{tmp}

\makeatletter
\def\whileexpr#1\do#2{%
  #1\expandafter\@iden\else\expandafter\@gobble\fi
  {#2\whileexpr#1\do{#2}}%
}
\makeatother

\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]\node[inner xsep=0pt] (#1) {};}
\newcommand\DrawLines[3][\baselineskip]{%
\\
\setcounter{tmp}{0}%
\begin{tikzpicture}[remember picture,overlay]
\foreach \i in {1,...,#2}
{\path[draw] let \p1=(#3), \p2=(current page text area.west) in ([yshift=-\i*#1]\x1,0|-0,\y1-\baselineskip) -- ( $ ([yshift=-\i*#1]current page text area.east|-#3) + (0,-\baselineskip) $ );}
\end{tikzpicture}%
\whileexpr\ifnum\thetmp<#2\relax\do{%
    \>\mbox{}\\[\dimexpr#1-\baselineskip\relax]\stepcounter{tmp}\relax
  }%
\>\mbox{}\\[3pt]
}

\begin{document}

\begin{tabbing}
\textbf{Question 1} \\
\\ 
\textbf{a.} \tab \=\tikzmark{A}Let $y = \left(- 3 x^{2} - 3 x\right)^{3}$. Find $\frac{dy}{dx}$.
\DrawLines{4}{A}%
\\
\textbf{b.} \tab\tikzmark{B}Let $f(x) = e^{2 x^{2} + 9 x + 5}$. Evaluate $f'(-1)$. \\ 
\\ 
\DrawLines[25pt]{7}{B}%
Let $f(x) = \left(- 3 x^{2} - 3 x\right)^{3} = u^{3}, u = - 3 x^{2} - 3 x$ \\ 
$f'(x) = 3 u^{2} \times u'$ \\ 
$f'(x) = - 81 x^{2} \left(x + 1\right)^{2} \left(2 x + 1\right)$ \\ 
\\
$f'(x) = \left(4 x + 9\right) e^{2 x^{2} + 9 x + 5}$ \\ 
$f'(-1) = \frac{5}{e^{2}}$ \\ 
\end{tabbing}

\end{document}

enter image description here

THe code needs two or three runs to stabilize.

I used the \whileexpr macro from Ahmed's Musa's answer to looping in tabular.

  • Wow this is really fantastic. I only have two questions - the first - is there a way to specify how much space will be left between the lines? I may want to change that in the future. The other is - how do I add a line of whitespace between when the question ends and the question lines begin, like in David Carlisle's answer? I am trying to manually add white space using "\\" or "\*" prior to using DrawLines, but TeXworks is bizarrely compiling that whitespace after DrawLines anyway. That or something else equally strange to me. – nebffa Jun 16 '13 at 6:22
  • To be clear, what seems to happen is the Latex compiles as expected the first time, with whitespace in the correct position, but upon recompiling it places the whitespace in the wrong position. Images of before and after to illustrate what happens. I assume this happens because some intermediate files are created in the first compilation that are used by further compilations if the original latex source file is unchanged. Is this something I should worry about? It's very strange to me... – nebffa Jun 16 '13 at 6:41
  • Further clarification, the only edit I make when doing this is verbatim copy-pasting your code, and then changing the "\\" prior to both DrawLines to "\[2in]" as a test – nebffa Jun 16 '13 at 6:44
  • @nebffa please see my updated answer; using an optional argument you can now control the vertical separation between lines. – Gonzalo Medina Jun 16 '13 at 17:14
  • Hey Gonzalo - I wish I could accept two answers because yours completely does what I want, however I've accepted David's answer because it is the simpler one (useful for a newbie like me) and the one I've used in my code. Thanks so much!! – nebffa Jun 19 '13 at 7:36
1

If it was for me, I would just use the booktabs package and get done with it.

A few weeks ago I was having the exam same problem, and instead of trying to work around a solution, booktabs was who simplify things at the maximum:

Look at this sample code:

\documentclass[12pt,a4paper]{article}

\usepackage{array}
\usepackage{booktabs}


\begin{document}

\begin{table}
\centering
\caption{Your caption here}\label{tab:yourlabel}
\begin{tabular}{lll}
\toprule
\textbf{Hola1} & \textbf{Hola2} & \textbf{Hola3}\\
\midrule
datum1 & datum2 &datum3\\
datum4 & datum5 &datum6\\
%\midrule
datum7 & datum8 &datum9\\
\bottomrule
\end{tabular}
\end{table}
\end{document}

Which produces this output:

enter image description here

The command \toprule serves as a horizontal line that looks different for marking the beginning of the table, whereas \midrule serves as a normal horizontal line that you can insert anywhere you want by just writting it (experiment uncommenting the one that is comment in my code). Finally, \bottomrule gives you another different line for marking the end of the table.

I'm not completely familiarize with the minor details, but I think these commands also present some type of advantages regarding the vertical space. For more information, please visit the booktabs documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.