4

I have a frame box next to some text (Komentar:), and I would like to stretch it so it fills up the page. I've done this using the calc package by subtracting \widthof{Komentar: } from \hsize (or \textwidth or \linewidth).

Komentar: \fbox{
    \newlength\mylength
    \setlength\mylength{\hsize - \widthof{Komentar: }}
    \begin{minipage}[t]{\mylength}
        \hfill
        \vspace{8cm}
    \end{minipage}
}

However, this stretches the box beyond what seems to be the right margin: I have two pieces of text with \hfill in between, and the box seems to go beyond that. In fact, it looks like the values returned by \hsize, \textwidth and \linewidth are wider than what \hfill makes it out to be. Here's a picture of what I mean:

enter image description here

So, either I'm not getting the page width correctly, or I'm doing the \hfill thing wrong. Either way, help is appreciated.

P.S. I also get a warning that the \hbox in paragraph at lines 84–92 is 17.89452pt too wide.


EDIT: Here's an MWE. You get the custom document class I'm using here.

\documentclass[a4paper, 12pt, master]{etf}
\usepackage{calc}
\begin{document}

{\setlength\parindent{0pt}
Komentar: \fbox{
    \newlength\mylength
    \setlength\mylength{\hsize - \widthof{Komentar: }}
    \begin{minipage}[t]{\mylength}
        \hfill
        \vspace{8cm}
    \end{minipage}
}

Datum odbrane: \underline{\hspace{7em}} \hfill
Ocena: \underline{\hspace{6em}} (\underline{\hspace{2em}})
}

\end{document}
  • Have you considered the \fboxsep length, which is small, but noticable and indicates the separation between the frame and the content? There is also the \fboxrule length, indicating the width of the frame rules. –  Apr 23 '14 at 15:08
  • Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Apr 23 '14 at 15:09
  • @Christian Subtracting \fboxsep from the length helped, but it didn't solve the issue. The thing with posting an MWE is that I'm using a custom document class, but I'll go ahead upload it somewhere and post an MWE then. – Konstantin Apr 23 '14 at 15:14
  • The smallest possible example is sufficient, just your geometry settings, e.g. –  Apr 23 '14 at 15:15
  • I've added an MWE. – Konstantin Apr 23 '14 at 15:24
  • You could just subtract another 18pt without worrying where the heck it came from. – John Kormylo Apr 23 '14 at 15:42
  • you don't need a custom class, you could make teh example use article – David Carlisle Apr 23 '14 at 15:51
  • BTW, a quick experiment found only 6.8pt of overhead for \fbox{\begin{minipage}{0pt}\end{minipage}} – John Kormylo Apr 23 '14 at 15:54

1 Answers1

5
{\setlength\parindent{0pt}
Komentar: \fbox{ %< there is a space here
 %< Doesn't introduce a space but never allocate a register inside a group
    \newlength\mylength 
    \setlength\mylength{\hsize - \widthof{Komentar: }} %< there is a space here
    \begin{minipage}[t]{\mylength}
        \hfill
        \vspace{8cm}
    \end{minipage} %< there is a space here
}

so on the line you have Komentar: and an fbox with content that is

\hsize - \widthof{Komentar: } + 2 word spaces

wide so the width of that box taking into account the rules and padding is

\hsize - \widthof{Komentar: } + 2 word spaces + 2\fboxsep + 2\fboxrule

Probably you want to comment out the ends of lines with % to avoid adding spurious space, and to account for 2\fboxsep + 2\fboxrule in the width of the box.

David Carlisle
  • 757,742
  • That did it, thanks. By the way, could you elaborate on why I shouldn't ever allocate a register inside a group? I assume you mean the {\setlength\parindent{0pt} ... } group. – Konstantin Apr 23 '14 at 16:49
  • @KonstantinĐ. Although you addressed David C. comment within his solution, I think he referred to possible side effects of allocating registers. Besides, using the Komentar code again, e.g. on next page, it leads to an error message about the already defined length \mylength –  Apr 23 '14 at 17:28
  • @KonstantinĐ. You should have \newlength{\mylength} in the document preamble and then you can set it to whatever value you need as many times as you want. – egreg Apr 23 '14 at 17:40
  • @egreg Are there any other advantages? Can't I declare as many newlengths as I want, with different names? I was thinking that I should keep all the code for this particular length together in one place. – Konstantin Apr 23 '14 at 18:00
  • @KonstantinĐ. You can declare as many lengths as you want (well, there's a limit on their number). Actually you don't need a new length for that application, because you can directly say \begin{minipage}[t]{\hsize - \widthof{Komentar: }} – egreg Apr 23 '14 at 18:02
  • @egreg Ahh, thank you, that didn't occur to me. – Konstantin Apr 23 '14 at 19:44
  • 1
    @KonstantinĐ. in classic tex there are only 256 length registers and latex already uses over half of them so they are a scarce resource. If you allocate inside a group the name goes out of scope but the allocation counter isn't reset so you just lose the register at the end of the group. (well it is still there but hard to access) – David Carlisle Apr 23 '14 at 20:30
  • 1
    A somewhat elegant solution is to use \widthof{\fbox{Komentar: }} – John Kormylo Apr 24 '14 at 00:46
  • @KonstantinĐ. I have recently written an answer to a question here about allocating/providing length registers: http://tex.stackexchange.com/questions/165312/looking-for-providelength/165318#165318 -- Perhaps it will help you concerning the allocation of \mylength –  Apr 24 '14 at 05:18
  • @ChristianHupfer but the providelength command there has the same problem that it must never be used inside a group. – David Carlisle Apr 24 '14 at 08:23
  • @DavidCarlisle: Excuse me, I was not precise enough: It was not meant to use it inside the group, but outside, for checking whether the desired length is already allocated or not. I use that command also, but of course, not inside a group. –  Apr 24 '14 at 08:25
  • @John This is what I ended up using, thanks John. – Konstantin Apr 28 '14 at 01:24