LaTeX forum ⇒ Text FormattingBoxes and Font size Topic is solved

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Boxes and Font size

Postby rspringuel » Mon Jan 11, 2016 9:26 pm

I'm writing a macro which is designed to allow the user to fill a box in a recursive fashion. I.e. if the box is empty, then the macro simply populates the box with the user designated material. If the box is not empty, then the new material is added to the old material on a new line (with alignment between the new and old material controlled by an optional argument). Spacing within the box is supposed to be controlled by some parameters which are set elsewhere, but I'm having some problems getting the spacing control right. There seems to be some extra vertical space being introduced.

Below you'll find my MWE, which has a stripped down version of the internal macro I'm writing. As you can see, there is extra space between the two lines of the material in the box when I use it (one example at normal text size, and another with [cmd]\scriptsize[/cmd] text because that introduces even more space).

In my attempts to work out what's going wrong, I tried stripping things down even further to produce the last two examples. These work, but I'm not sure what the difference between them and the macro is. As best I can tell, I've reproduced all of the boxing steps just with fixed values for the parameters and without recursive function calling. They clearly show that what I want to do is possible. How do I get my macro to produce what these later examples are showing is possible (though without the customizable parameters).

Note: While I don't believe it makes a difference here, the package which this macro appears in is targeted for LuaLaTeX and LuaTeX (it is compatible with both).


  1. % !TEX program = LuaLaTeX+se
  2. \documentclass{report}
  3.  
  4. \makeatletter
  5. \newbox\gre@box@temp@width%
  6. \newdimen\gre@dimen@temp@three
  7.  
  8. \newdimen\gre@dimen@annotationseparation
  9. \gre@dimen@annotationseparation = 0pt
  10.  
  11. \def\gre@widthof#1{%
  12. \setbox\gre@box@temp@width=\hbox{#1}%
  13. \global\gre@dimen@temp@three=\wd\gre@box@temp@width%
  14. \relax%
  15. }%
  16.  
  17. \newbox \gre@box@annotation@old
  18. \newbox \gre@box@annotation@add
  19. \newbox \gre@box@annotation
  20.  
  21. \def\gre@annotation[#1]#2{%
  22. \ifx l#1\relax%
  23. \let\gre@rightfill\hfil%
  24. \let\gre@leftfill\relax%
  25. \else\ifx r#1\relax%
  26. \let\gre@rightfill\relax%
  27. \let\gre@leftfill\hfil%
  28. \else\ifx c#1\relax%
  29. \let\gre@rightfill\hfil%
  30. \let\gre@leftfill\hfil%
  31. \else%
  32. \gre@error{Unrecognzied alignment option for \protect\greannotation}%
  33. \fi\fi\fi%
  34. % first we determine the widest box
  35. \gre@widthof{#2}%
  36. \ifdim\gre@dimen@temp@three < \wd\gre@box@annotation\relax%
  37. \gre@dimen@temp@three = \wd\gre@box@annotation\relax%
  38. \fi%
  39. \setbox\gre@box@annotation@old = \hbox to \gre@dimen@temp@three{\gre@leftfill\copy\gre@box@annotation\gre@rightfill}%
  40. \setbox\gre@box@annotation@add = \hbox to \gre@dimen@temp@three{\gre@leftfill#2\gre@rightfill}%
  41. \ifvoid\gre@box@annotation%
  42. \setbox\gre@box@annotation = \hbox to \gre@dimen@temp@three{\vtop{{\offinterlineskip\vss\noindent\box\gre@box@annotation@add}}}%
  43. \else%
  44. \setbox\gre@box@annotation = \hbox to \gre@dimen@temp@three{\vtop spread \gre@dimen@annotationseparation\relax{{\offinterlineskip\noindent\box\gre@box@annotation@old\vss\noindent\box\gre@box@annotation@add}}}%
  45. \fi%
  46. }%
  47.  
  48.  
  49. \begin{document}
  50.  
  51. \gre@annotation[c]{Test}
  52. \gre@annotation[c]{Also Test}
  53.  
  54. |\box\gre@box@annotation|
  55.  
  56. \bigskip
  57. \noindent\rule{\textwidth}{0.4pt}
  58. \bigskip
  59.  
  60. \gre@annotation[c]{\scriptsize Test}
  61. \gre@annotation[c]{\scriptsize Also Test}
  62.  
  63. |\box\gre@box@annotation|
  64.  
  65. \bigskip
  66. \noindent\rule{\textwidth}{0.4pt}
  67. \bigskip
  68.  
  69. \newbox\original
  70. \newbox\old
  71. \newbox\intermediate
  72. \newbox\new
  73. \newbox\combine
  74.  
  75. \setbox\original = \hbox to 1in{\hfil Test\hfil}
  76. \setbox\intermediate = \hbox to 1in{\vtop{{\offinterlineskip\vss\noindent\box\original}}}
  77. \setbox\old = \hbox to 1in {\hfil\copy\intermediate\hfil}
  78. \setbox\new = \hbox to 1in {\hfil Also Test\hfil}
  79. \setbox\combine = \hbox to 1in {\vtop spread 0pt {\offinterlineskip\noindent\box\old\vss\noindent\box\new}}
  80.  
  81. |\box\combine|
  82.  
  83. \bigskip
  84. \noindent\rule{\textwidth}{0.4pt}
  85. \bigskip
  86.  
  87. \setbox\original = \hbox to 1in{\hfil\scriptsize Test\hfil}
  88. \setbox\intermediate = \hbox to 1in{\vtop{{\offinterlineskip\vss\noindent\box\original}}}
  89. \setbox\old = \hbox to 1in {\hfil\copy\intermediate\hfil}
  90. \setbox\new = \hbox to 1in {\hfil \scriptsize Also Test\hfil}
  91. \setbox\combine = \hbox to 1in {\vtop spread 0pt {\offinterlineskip\noindent\box\old\vss\noindent\box\new}}
  92.  
  93. |\box\combine|
  94.  
  95.  
  96. \end{document}

Tags:

rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Postby rspringuel » Wed Jan 13, 2016 4:36 am

So, with some time to spare today for this problem, I decided to try rewriting the macro from scratch (instead of adapting the existing code) and was able to solve my problem. Below is a MWE which shows the macro working as expected. The new version of the macro doesn't contain quite so many embedded boxes, which makes me think that it was the process of embedding boxes within boxes that somewhere along the line led to the problem.

It just goes to show that sometimes you're better off starting from scratch rather than trying to adapt old code.

I still don't understand why the original code didn't work as intended though, so if some one has an explanation for that I'm interested in learning more.

  1. % !TEX program = LuaLaTeX+se
  2. \documentclass{report}
  3.  
  4. \makeatletter
  5. \newbox\gre@box@temp@width%
  6. \newdimen\gre@dimen@temp@three
  7.  
  8. \newdimen\gre@dimen@annotationseparation
  9. \gre@dimen@annotationseparation = 0pt
  10.  
  11. \def\gre@widthof#1{%
  12. \setbox\gre@box@temp@width=\hbox{#1}%
  13. \global\gre@dimen@temp@three=\wd\gre@box@temp@width%
  14. \relax%
  15. }%
  16.  
  17. \newbox \gre@box@annotation@old
  18. \newbox \gre@box@annotation@add
  19. \newbox \gre@box@annotation
  20.  
  21. \def\gre@annotation[#1]#2{%
  22. \ifx l#1\relax%
  23. \let\gre@rightfill\hfil%
  24. \let\gre@leftfill\relax%
  25. \else\ifx r#1\relax%
  26. \let\gre@rightfill\relax%
  27. \let\gre@leftfill\hfil%
  28. \else\ifx c#1\relax%
  29. \let\gre@rightfill\hfil%
  30. \let\gre@leftfill\hfil%
  31. \else%
  32. \gre@error{Unrecognzied alignment option for \protect\greannotation}%
  33. \fi\fi\fi%
  34. % first we determine the widest box
  35. \gre@widthof{#2}%
  36. \ifdim\gre@dimen@temp@three < \wd\gre@box@annotation\relax%
  37. \gre@dimen@temp@three = \wd\gre@box@annotation\relax%
  38. \fi%
  39. \ifvoid\gre@box@annotation%
  40. \setbox\gre@box@annotation = \hbox to \gre@dimen@temp@three{\gre@leftfill#2\gre@rightfill}%
  41. \else%
  42. \setbox\gre@box@annotation@old = \hbox to \gre@dimen@temp@three{\gre@leftfill\box\gre@box@annotation\gre@rightfill}
  43. \setbox\gre@box@annotation@add = \hbox to \gre@dimen@temp@three{\gre@leftfill#2\gre@rightfill}%
  44. \setbox\gre@box@annotation = \vtop spread \gre@dimen@annotationseparation\relax {\offinterlineskip\box\gre@box@annotation@old\vss\box\gre@box@annotation@add}%
  45. \fi%
  46. }%
  47.  
  48.  
  49. \begin{document}
  50.  
  51. \gre@annotation[c]{Test}
  52. \gre@annotation[c]{Also Test}
  53.  
  54. |\box\gre@box@annotation|
  55.  
  56. \bigskip
  57. \noindent\rule{\textwidth}{0.4pt}
  58. \bigskip
  59.  
  60. \gre@annotation[c]{\scriptsize Test}
  61. \gre@annotation[c]{\scriptsize Also Test}
  62.  
  63. |\box\gre@box@annotation|
  64.  
  65. \end{document}


Return to “Text Formatting”

Who is online

Users browsing this forum: Baidu [Spider] and 6 guests