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}
8
  • 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.
    – user31729
    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}.
    – user31729
    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.
    – user31729
    Apr 23 '14 at 15:15
  • I've added an MWE.
    – Konstantin
    Apr 23 '14 at 15:24
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.

12
  • 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
    – user31729
    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
  • 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) Apr 23 '14 at 20:30
  • 1
    A somewhat elegant solution is to use \widthof{\fbox{Komentar: }} Apr 24 '14 at 0:46

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.