2

I had this code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}    
\newcommand{\mywidth}{\linewidth}
\begin{figure}[p]
\begin{tikzpicture}
\draw[ultra thick] (0,0) rectangle (\mywidth,2); %this line produces the overfull \hbox
\node[draw,minimum width=4cm,minimum height=1cm] at (\mywidth-4cm/2,1cm/2) {right lower corner};
\node[draw,minimum width=1cm,minimum height=1cm] at (\mywidth/2,1cm/2) {middle};
\end{tikzpicture}
\end{figure}
\end{document}

which gave me an overfull \hbox (1.6pt too wide)

so I wanted to change \newcommand{\mywidth}{\linewidth-1.6pt} but then the parenthesis in the calculations don't work.

So I changed every \mywidth/2 by {(\mywidth/2)} where { } alwayshas to surround the whole expression.

My actual tikzpicture is much more complicated containing many formulas that build on \mywidth and it is lots of work to change all of them.

Is there a more elegant way to get rid of this overfull \hbox without destroying all the alignments within the graphic?

Can I somehow put parenthesis inside \newcommand{\mywidth}{\linewidth-1.6pt}?

Bonus question. What happens when I change ultra thick to very thick? This gives me a very uggly number in the overfull \hbox warning.

  • You need to subtract half the line width from each ends: \draw[ultra thick] (\pgflinewidth/2,0) rectangle (\mywidth-\pgflinewidth/2,2);. – user194703 Feb 6 at 18:08
3

My proposal is:

  1. replace your rectangle by a node;
  2. create a style that specify the "exterior size" of the nodes;
  3. use anchors to position your nodes.
\documentclass{article}
\usepackage{tikz}
\tikzset{
  exterior size/.style args={#1x#2}{
    outer sep=auto,
    minimum width={#1-\pgflinewidth},
    minimum height={#2-\pgflinewidth}
  }
}
\begin{document}
  \begin{figure}[p]
    \begin{tikzpicture}[nodes=draw]
      \path[ultra thick] (0,0) node[exterior size=\linewidth x 2cm, anchor=south west](R){};
      \node[red, line width=1.5mm, exterior size=4cm x 1cm, anchor=south east] at (R.south east) {right lower corner};
      \node[green, exterior size=4cm x 1cm, anchor=south] at (R.south) {middle};
    \end{tikzpicture}
  \end{figure}
\end{document}

enter image description here

| improve this answer | |
  • I give my welcome for your long absence. on TeX.SE. – Sebastiano Feb 6 at 20:34
  • 1
    @Sebastiano Thanks, but probably it will be a short stay. – Kpym Feb 6 at 21:51
1

You need to subtract half the line width from each ends, and can use anchors for easier positioning without using computations.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning} %<- not used here

\begin{document}    
\begin{figure}[p]
\begin{tikzpicture}
\draw[ultra thick] (\pgflinewidth/2,0)  rectangle (\linewidth-\pgflinewidth/2,2); %this line produces the overfull \hbox
\node[draw,minimum width=4cm,minimum height=1cm,anchor=south east] at (\linewidth,0) {right lower corner};
\node[draw,minimum width=1cm,minimum height=1cm,anchor=south] at (\linewidth/2,0) {middle};
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

| improve this answer | |

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.