3

I was trying to create a long horizontal bar where to put the marks of an assessment and I had created the following code.

\documentclass{article}

\usepackage{tikz} \setlength{\parindent}{0pt}

\newcommand{\TotalMarks}[1]{% \tikz\draw[thick] (0,0) -- (\linewidth,0) -- ++(0,2em) -- ++(-2em,0) -- ++(0,-2em) node[pos=0.6,left] {\makebox[2.9cm]{\textbf{Total: #1 marks}\hfill}};}

\begin{document} \TotalMarks{2} \end{document}

I cannot understand why it gives an Overfull error:

Overfull \hbox (0.79999pt too wide) in paragraph at lines 14--15[][] 

How can I fix it?

Thank you for any help you may give me.

Logos
  • 419

1 Answers1

5

There are two things that cause this:

  1. Paragraph indentation. The standard paragraph indentation is added before the tikzpicture, you can disable that for a single line with \noindent, i.e. \noindent\tikz...

  2. When you draw a line, the bounding box will actually extend by half the line width beyond the end coordinates, as you can see from this example:

     \documentclass[border=5mm]{standalone}
     \usepackage{tikz}
     \begin{document}
    

    \begin{tikzpicture} \draw [line width=5mm] (0,0) -- (1,0); \draw [very thin, red] (0,-0.5) -- (0,0.5);

    \draw [thin, blue] (current bounding box.south east) rectangle (current bounding box.north west); \end{tikzpicture} \end{document}

enter image description here

The blue line indicates the bounding box.

If you add line cap=rect, the line will extend to fill that blank space, and you pgf has the handy macro \pgflinewidth that has the width of the current path. Hence, you can do \draw[thick,line cap=rect] (0,0) -- (\linewidth-\pgflinewidth,0) ....

Working example, as egreg mentioned you shouldn't use minimal (Why should the minimal class be avoided?):

\documentclass{article}

\usepackage{tikz}

\newcommand{\TotalMarks}[1]{% \tikz\draw[thick,line cap=rect] (0,0) -- (\linewidth-\pgflinewidth,0) -- ++(0,2em) -- ++(-2em,0) -- ++(0,-2em) node[pos=0.6,left] {\makebox[2.9cm]{\textbf{Total: #1 marks}\hfill}};}

\begin{document} \noindent\TotalMarks{2} \end{document}

Torbjørn T.
  • 206,688
  • 1
    Sorry, didn't see the comment about the paragraph indentation before posting the answer, but point 2 covers the remaining 0.8pt. – Torbjørn T. Sep 12 '20 at 21:08
  • 1
    This works brilliantly! Such a good explanation, thank you very much about it! – Logos Sep 12 '20 at 21:09