4

I need to be able to underline equations involving fractions, which are aligned using the alignedat environment from amsmath. To be able to underline at all, I'm using the solution from this question - but when fractions get 'underlined', the line goes through the middle of the fraction rather than beneath it.

Also, is it possible to modify the underlining to make it a double underline?

Code - main file

\documentclass[answers]{exam}

\addtolength{\textwidth}{1cm}
\addtolength{\hoffset}{-0.5cm}
\addtolength{\textheight}{1cm}
\addtolength{\voffset}{-0.5cm}

\usepackage{xcolor} % Use colour!
\usepackage{titlesec} % Allow creation of new sectioning commands
\usepackage{amsmath} % Needed for splitting equations over multiple lines
\usepackage[normalem]{ulem} % Underlining effects
\usepackage{environ} % Tinkering with environments
\usepackage{gensymb} % Degree symbol
\usepackage[pdfstartview=FitH,bookmarks=true]{hyperref} 
\usepackage[numbered]{bookmark} % PDF Bookmarks
\usepackage{tikz} % Underlining spanning alignment marks

\titleformat{\section}{\normalfont\large\bfseries}{Chapter }{0em}{\thesection: }
\renewcommand\sectionmark[1]{\markright{#1}} % Set up \rightmark
\renewcommand{\thesection}{\texorpdfstring{}{Chapter }\arabic{section}\texorpdfstring{}{:}} % Correct text for bookmarks

\titleclass{\exercise}{straight}[\section]  % Create new 'section' command for exercises - this replaces \subsection
\newcounter{exercise}
\titleformat{\exercise}{\normalfont\large\bfseries}{Exercise }{0em}{\theexercise}
\titlespacing*{\exercise}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\exerciseautorefname}{exercise}
\renewcommand{\theexercise}{\texorpdfstring{}{Exercise }\arabic{section}\Alph{exercise}} % Correct text for bookmarks
\makeatletter
\providecommand*{\toclevel@exercise}{2} % Make sure exercises appear below sections in bookmarks
\makeatother

\renewcommand{\thepartno}{\roman{partno}} % Format part numbers as lower-case roman numerals
\renewcommand{\solutiontitle}{\noindent} % Format solution - just print solution as entered
%\newcommand{\ans}[1]{\uuline{#1}} % Double underline final answer
\newcommand{\ans}[1]{\tikzmarkin{\theexercise\thequestion\thepartno}#1\tikzmarkend{\theexercise\thequestion\thepartno}} % Double underline final answer

\pagestyle{headandfoot} % Let's have both
\header{\oddeven{Exercise \theexercise}{Chapter \thesection: \rightmark}}{Core 1}{\oddeven{Chapter \thesection: \rightmark}{Exercise \theexercise}}
\headrule
\footer{\oddeven{}{\thepage}}{}{\oddeven{\thepage}{}}
\footrule


\NewEnviron{sol}{ % Align solutions sensibly
    \begin{solution}
        $\begin{alignedat}{2}
            \BODY
        \end{alignedat}$
    \end{solution}
}


\makeatletter % Use tikzmarks to underline across alignment marks
\tikzset{%
     remember picture with id/.style={%
       remember picture,
       overlay,
       save picture id=#1,
     },
     save picture id/.code={%
       \edef\pgf@temp{#1}%
       \immediate\write\pgfutil@auxout{%
         \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
     },
     if picture id/.code args={#1#2#3}{%
       \@ifundefined{save@pt@#1}{%
         \pgfkeysalso{#3}%
       }{
         \pgfkeysalso{#2}%
       }
     }
   }

   \def\savepointas#1#2{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}

\def\tmk@labeldef#1,#2\@nil{%
  \def\tmk@label{#1}%
  \def\tmk@def{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \tikz@scan@one@point\pgfutil@firstofone\tmk@def
  }{%
  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}
\makeatother

\newcommand{\tikzmarkin}[2][]{%
      \tikz[remember picture,overlay,baseline=0.25ex]
      \draw[line width=0.3pt,#1]
      (pic cs:#2) -- (0,0)
      ;}

\newcommand\tikzmarkend[2][]{%
\tikz[remember picture with id=#2,baseline=0.25ex] #1;}


\begin{document}

\unframedsolutions % Print solutions as plain text
\SolutionEmphasis{\color{blue}} % Print solutions in blue

\include{Chapter1}

\end{document}

Code - Chapter1.tex

\section{Basic Algebra}
\begin{questions}
    \question Simplify the following fractions as much as possible.
    \begin{parts}
        \part $\dfrac{ab}{ac}$
        \begin{sol}
            \ans{\dfrac{ab}{ac} &= \dfrac{b}{c}}
        \end{sol}
    \end{parts}
\end{questions}
  • 1
    This is surely not a MWE. I get regular double underlining if I use \underline{\underline{#1}} in the definition of \ans. The obvious problem is that the "tikzmark" is placed at the baseline. – egreg May 19 '13 at 19:14
  • @egreg My mistake, the \ans should wrap around the whole equation (code corrected to show this). I can see that's what the problem is, what I can't see is what the solution should be... – JustinT May 19 '13 at 19:38
  • It isn't at all clear what the intention of the markup. Why sol (solution?) environment and \ans (answer?) command. I ask as you appear to want to add underlining to \ans which is harder as it is inside the alignment rather than to sol where it is trivial to wrap the entire math in one or two underline. – David Carlisle Jun 24 '13 at 14:31
2

Well. I've found a way to do it, but it's by no means a good solution.

Code - main file

\documentclass[answers]{exam}

\addtolength{\textwidth}{1cm}
\addtolength{\hoffset}{-0.5cm}
\addtolength{\textheight}{1cm}
\addtolength{\voffset}{-0.5cm}

\usepackage{xcolor} % Use colour!
\usepackage{titlesec} % Allow creation of new sectioning commands
\usepackage{amsmath} % Needed for splitting equations over multiple lines
\usepackage[normalem]{ulem} % Underlining effects
\usepackage{environ} % Tinkering with environments
\usepackage{gensymb} % Degree symbol
\usepackage[pdfstartview=FitH,bookmarks=true]{hyperref} 
\usepackage[numbered]{bookmark} % PDF Bookmarks
\usepackage{tikz} % Underlining spanning alignment marks
\usepackage{etoolbox} % Control sequences

\titleformat{\section}{\normalfont\large\bfseries}{Chapter }{0em}{\thesection: }
\renewcommand\sectionmark[1]{\markright{#1}} % Set up \rightmark
\renewcommand{\thesection}{\texorpdfstring{}{Chapter }\arabic{section}\texorpdfstring{}{:}} % Correct text for bookmarks

\titleclass{\exercise}{straight}[\section]  % Create new 'section' command for exercises - this replaces \subsection
\newcounter{exercise}
\titleformat{\exercise}{\normalfont\large\bfseries}{Exercise }{0em}{\theexercise}
\titlespacing*{\exercise}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\exerciseautorefname}{exercise}
\renewcommand{\theexercise}{\texorpdfstring{}{Exercise }\arabic{section}\Alph{exercise}} % Correct text for bookmarks
\makeatletter
\providecommand*{\toclevel@exercise}{2} % Make sure exercises appear below sections in bookmarks
\makeatother

\renewcommand{\thepartno}{\roman{partno}} % Format part numbers as lower-case roman numerals
\renewcommand{\solutiontitle}{\noindent} % Format solution - just print solution as entered
\newcommand{\ans}[2][0]{% Double underline answers to equations
    \tikzmarkin[#1ex]{\theexercise\thequestion\thepartno\thefigno a}
    \tikzmarkin[#1ex+0.35ex]{\theexercise\thequestion\thepartno\thefigno b}
    #2
    \tikzmarkend[#1ex+0.35ex]{\theexercise\thequestion\thepartno\thefigno b}
    \tikzmarkend[#1ex]{\theexercise\thequestion\thepartno\thefigno a}
}
\pagestyle{headandfoot} % Let's have both
\header{\oddeven{Exercise \theexercise}{Chapter \thesection: \rightmark}}{Core 1}{\oddeven{Chapter \thesection: \rightmark}{Exercise \theexercise}}
\headrule
\footer{\oddeven{}{\thepage}}{}{\oddeven{\thepage}{}}
\footrule


\NewEnviron{sol}{ % Align solutions sensibly
    \begin{solution}
        $\begin{alignedat}{2}
            \BODY
        \end{alignedat}$
    \end{solution}
}


\makeatletter % Use tikzmarks to underline across alignment marks
\tikzset{%
     remember picture with id/.style={%
       remember picture,
       overlay,
       save picture id=#1,
     },
     save picture id/.code={%
       \edef\pgf@temp{#1}%
       \immediate\write\pgfutil@auxout{%
         \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
     },
     if picture id/.code args={#1#2#3}{%
       \@ifundefined{save@pt@#1}{%
         \pgfkeysalso{#3}%
       }{
         \pgfkeysalso{#2}%
       }
     }
   }

   \def\savepointas#1#2{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}

\def\tmk@labeldef#1,#2\@nil{%
  \def\tmk@label{#1}%
  \def\tmk@def{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \tikz@scan@one@point\pgfutil@firstofone\tmk@def
  }{%
  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}
\makeatother

\newcommand{\tikzmarkin}[2][0.25ex]{%
      \tikz[remember picture,overlay,baseline=#1]
      \draw[line width=0.4pt]
      (pic cs:#2) -- (0,0)
      ;}

\newcommand\tikzmarkend[2][0.25ex]{%
\tikz[remember picture with id=#2,baseline=#1];}


\begin{document}

\unframedsolutions % Print solutions as plain text
\SolutionEmphasis{\color{blue}} % Print solutions in blue

\include{Chapter1}

\end{document}

I'm sure there a far better solution, doing something like \uuline from the ulem package to determine the correct vertical position for the underlines, but using tikz to avoid being broken by alignment characters. Unfortunately my 'expertise' doesn't run quite that far!


Edited with updated solution - the [optional] first parameter is the vertical distance to lower the underlining by (measured in exs).

  • I get ! LaTeX Error: No counter 'figno' defined. – David Carlisle Jun 24 '13 at 14:23
  • Oops! Yes, that line shouldn't have been there. Thanks for pointing it out! – JustinT Jun 25 '13 at 8:03

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.