Fitting and centering text (both!) in a constrained area












10















Is there a way to make text size and alignment automatically "best fit" on a page? So that a large amount of text will be constrained on the page in a small font, but a small amount of text can be of larger font and centered properly?



The best way I found to fit lots of text on a page is with:



begin{minipage}{width}
...
end{minipage}
newpage


...but when there is a smaller amount of text, the font remains small and is awkwardly aligned at the top of the page, whereas I'd like it to be centered vertically and horizontally. I tried vfill with minipage, but it makes no difference.



For smaller amounts of text, I succeeded with:



mbox{}
vfill
begin{center}
....
end{center}
vfill
newpage


This centered the text perfectly, but I couldn't constrain a large block to one page with this. I tried combining minipage and mbox, but minipage overrode the vfill spacing and the alignment was screwed up again for small amounts of text.










share|improve this question

























  • What is the point of the minipage in your first listing?

    – TH.
    Aug 24 '10 at 4:54











  • Related: tex.stackexchange.com/questions/123614/…

    – Steven B. Segletes
    Apr 23 '15 at 13:32
















10















Is there a way to make text size and alignment automatically "best fit" on a page? So that a large amount of text will be constrained on the page in a small font, but a small amount of text can be of larger font and centered properly?



The best way I found to fit lots of text on a page is with:



begin{minipage}{width}
...
end{minipage}
newpage


...but when there is a smaller amount of text, the font remains small and is awkwardly aligned at the top of the page, whereas I'd like it to be centered vertically and horizontally. I tried vfill with minipage, but it makes no difference.



For smaller amounts of text, I succeeded with:



mbox{}
vfill
begin{center}
....
end{center}
vfill
newpage


This centered the text perfectly, but I couldn't constrain a large block to one page with this. I tried combining minipage and mbox, but minipage overrode the vfill spacing and the alignment was screwed up again for small amounts of text.










share|improve this question

























  • What is the point of the minipage in your first listing?

    – TH.
    Aug 24 '10 at 4:54











  • Related: tex.stackexchange.com/questions/123614/…

    – Steven B. Segletes
    Apr 23 '15 at 13:32














10












10








10


1






Is there a way to make text size and alignment automatically "best fit" on a page? So that a large amount of text will be constrained on the page in a small font, but a small amount of text can be of larger font and centered properly?



The best way I found to fit lots of text on a page is with:



begin{minipage}{width}
...
end{minipage}
newpage


...but when there is a smaller amount of text, the font remains small and is awkwardly aligned at the top of the page, whereas I'd like it to be centered vertically and horizontally. I tried vfill with minipage, but it makes no difference.



For smaller amounts of text, I succeeded with:



mbox{}
vfill
begin{center}
....
end{center}
vfill
newpage


This centered the text perfectly, but I couldn't constrain a large block to one page with this. I tried combining minipage and mbox, but minipage overrode the vfill spacing and the alignment was screwed up again for small amounts of text.










share|improve this question
















Is there a way to make text size and alignment automatically "best fit" on a page? So that a large amount of text will be constrained on the page in a small font, but a small amount of text can be of larger font and centered properly?



The best way I found to fit lots of text on a page is with:



begin{minipage}{width}
...
end{minipage}
newpage


...but when there is a smaller amount of text, the font remains small and is awkwardly aligned at the top of the page, whereas I'd like it to be centered vertically and horizontally. I tried vfill with minipage, but it makes no difference.



For smaller amounts of text, I succeeded with:



mbox{}
vfill
begin{center}
....
end{center}
vfill
newpage


This centered the text perfectly, but I couldn't constrain a large block to one page with this. I tried combining minipage and mbox, but minipage overrode the vfill spacing and the alignment was screwed up again for small amounts of text.







minipage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 23 '15 at 13:31









Svend Tveskæg

20.8k1051139




20.8k1051139










asked Aug 24 '10 at 0:59









hypatiahypatia

5315




5315













  • What is the point of the minipage in your first listing?

    – TH.
    Aug 24 '10 at 4:54











  • Related: tex.stackexchange.com/questions/123614/…

    – Steven B. Segletes
    Apr 23 '15 at 13:32



















  • What is the point of the minipage in your first listing?

    – TH.
    Aug 24 '10 at 4:54











  • Related: tex.stackexchange.com/questions/123614/…

    – Steven B. Segletes
    Apr 23 '15 at 13:32

















What is the point of the minipage in your first listing?

– TH.
Aug 24 '10 at 4:54





What is the point of the minipage in your first listing?

– TH.
Aug 24 '10 at 4:54













Related: tex.stackexchange.com/questions/123614/…

– Steven B. Segletes
Apr 23 '15 at 13:32





Related: tex.stackexchange.com/questions/123614/…

– Steven B. Segletes
Apr 23 '15 at 13:32










3 Answers
3






active

oldest

votes


















13














Okay, TeX isn't really designed with this in mind. That said, it can be done (sort of).



documentclass{article}
usepackage{lmodern}
newdimenfontdim
newdimenupperfontdim
newdimenlowerfontdim
newififmoreiterations
fontdim12pt
makeatletter
defbuildbox{%
setbox0vbox{fontsize{fontdim}{1.2fontdim}%
selectfont
centering
stuff}%
dimen@ht0
advancedimen@dp0
%message{Total height: thedimen@^^J}
}

newcommandfillthepage[1]{%
protected@edefstuff{#1}%
buildbox
% Compute upper and lower bounds
ifdimdimen@>textheight
loop
fontdim.5fontdim
buildbox
ifdimdimen@>textheight
repeat
lowerfontdimfontdim
upperfontdim2fontdim
fontdim1.5fontdim
else
loop
fontdim2fontdim
buildbox
ifdimdimen@<textheight
repeat
upperfontdimfontdim
lowerfontdim.5fontdim
fontdim.75fontdim
fi
% Now try to find the optimum size
loop
%message{Bounds: thelowerfontdimspace
% thefontdimspace theupperfontdim^^J}
buildbox
ifdimdimen@>textheight
moreiterationstrue
upperfontdimfontdim
advancefontdimlowerfontdim
fontdim.5fontdim
else
advancedimen@-textheight
ifdimdimen@<10pt
lowerfontdimfontdim
advancefontdimupperfontdim
fontdim.5fontdim
dimen@upperfontdim
advancedimen@-lowerfontdim
ifdimdimen@<.2pt
moreiterationsfalse
else
moreiterationstrue
fi
else
moreiterationsfalse
fi
fi
ifmoreiterations
repeat
% build the page
newpage
null
vfill
box0
vfill
}
makeatother
usepackage{lipsum}
begin{document}
fillthepage{lipsum}
fillthepage{lipsum[1]}
fillthepage{Very little}
end{document}


You can uncomment out the messages if you want to see how it decides on the font sizes to use. It doesn't make any effort to constrain horizontal size, so if you don't give it enough text to work with, you'll run off the right side of the page. (Try fillthepage{X} for an example of that.)



It's pretty much essential that you use a vector font, otherwise it'll tend to go back and forth between two font sizes, spewing warnings and probably never converge. That's why I have the usepackage{lmodern}.



I'm sure there are (many) cases where this doesn't work, but it was sort of fun to write.



Edit: Maybe that protected@edef should just be a def. I was originally thinking expanding it would be better, but now I'm not so sure






share|improve this answer





















  • 1





    +1, TH. It doesn't happen every day but today sure was one of them. Congrats, your answer left me smiling from ear to ear. :))

    – Geoffrey Jones
    Aug 24 '10 at 7:25











  • Thanks. =) The best part is, someone who's actually good at TeX will point out the trivial solution or package that someone has already written to handle this sort of thing.

    – TH.
    Aug 24 '10 at 7:30











  • Thanks so much, TH! Your code worked perfectly. I'm very new to tex and there's no way I would have been able to piece something like that together myself. :)

    – hypatia
    Aug 24 '10 at 17:33











  • This is very cool! One clarification before I open a separate question: How would I constrain it to a box of given dimensions? For the height, I can surely replace textheight with a parameter, but how do I constrain the width?

    – kongo09
    Aug 17 '11 at 16:24











  • @kongo09, If I were to rewrite this, I'd I'd replace the null vfillbox0vfill with vbox totextheight{vfillbox0 vfill}, as well as replace the protected@edef with just a def and all the 0s with z@ for that matter. And of course, you'd replace all textheight (including the one I just added in this comment) with a parameter. You can give buildbox a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.

    – TH.
    Aug 19 '11 at 4:13



















1














I'm not quite sure what you are actually trying to do. But "but minipage overrode the vfill spacing" sounds as if you have put the vfill inside the minipage. This makes only sense if you give the minipage a specific height (which is possible with an optional argument). If you want to move the minipage itself the vfill must go outside.



You can use varwidth to get a minipage which adjust its width to its content. You can use resizebox to scale a minipage to a given width.



Btw: Center things horizontally is in general a well-defined conzept: Chars, boxes and words have only one width and the reference point one wants to use is clear. This is different when you want to center things vertically. Chars and boxes have height and depth. Do you want to center a "A" along the baseline or along a line somewhere in the middle of the char? You can see the problem if you remove the [b] in the last example.



documentclass{article}
usepackage{varwidth,lipsum,graphics}
begin{document}
vspace*{fill}

{centering
begin{minipage}{0.5textwidth}
lipsum[1]
end{minipage}
par}


vfill

newpage
vspace*{fill}

{centering
begin{varwidth}{0.5textwidth}
abc
end{varwidth}
par}


vfill
newpage
vspace*{fill}

{centering
begin{minipage}{0.5textwidth}
centering abc\cdeaddff
end{minipage}
par}


vfill

newpage
vspace*{fill}

{centering
resizebox{textwidth}{!}{%
.begin{varwidth}[b]{0.5textwidth}
abc
end{varwidth}}
par}
vfill
end{document}





share|improve this answer































    1














    fitting library from tcolorbox allows to adjust a given text to some width and height. Default font size is never enlarged, but you can start with a large font size (fit basedim) which will be reduced until all text fits in desired box.



    documentclass{article}
    usepackage{lmodern}
    usepackage[most]{tcolorbox}
    usepackage{lipsum}

    tcbset{valign=center, halign=center}

    begin{document}
    begin{tcolorbox}[fit to height=.4textheight, fit basedim=50pt]
    lipsum[1]
    end{tcolorbox}

    begin{tcolorbox}[fit to height=.4textheight, fit basedim=50pt]
    lipsum[1-5]
    end{tcolorbox}
    end{document}


    enter image description here






    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "85"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f2231%2ffitting-and-centering-text-both-in-a-constrained-area%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      13














      Okay, TeX isn't really designed with this in mind. That said, it can be done (sort of).



      documentclass{article}
      usepackage{lmodern}
      newdimenfontdim
      newdimenupperfontdim
      newdimenlowerfontdim
      newififmoreiterations
      fontdim12pt
      makeatletter
      defbuildbox{%
      setbox0vbox{fontsize{fontdim}{1.2fontdim}%
      selectfont
      centering
      stuff}%
      dimen@ht0
      advancedimen@dp0
      %message{Total height: thedimen@^^J}
      }

      newcommandfillthepage[1]{%
      protected@edefstuff{#1}%
      buildbox
      % Compute upper and lower bounds
      ifdimdimen@>textheight
      loop
      fontdim.5fontdim
      buildbox
      ifdimdimen@>textheight
      repeat
      lowerfontdimfontdim
      upperfontdim2fontdim
      fontdim1.5fontdim
      else
      loop
      fontdim2fontdim
      buildbox
      ifdimdimen@<textheight
      repeat
      upperfontdimfontdim
      lowerfontdim.5fontdim
      fontdim.75fontdim
      fi
      % Now try to find the optimum size
      loop
      %message{Bounds: thelowerfontdimspace
      % thefontdimspace theupperfontdim^^J}
      buildbox
      ifdimdimen@>textheight
      moreiterationstrue
      upperfontdimfontdim
      advancefontdimlowerfontdim
      fontdim.5fontdim
      else
      advancedimen@-textheight
      ifdimdimen@<10pt
      lowerfontdimfontdim
      advancefontdimupperfontdim
      fontdim.5fontdim
      dimen@upperfontdim
      advancedimen@-lowerfontdim
      ifdimdimen@<.2pt
      moreiterationsfalse
      else
      moreiterationstrue
      fi
      else
      moreiterationsfalse
      fi
      fi
      ifmoreiterations
      repeat
      % build the page
      newpage
      null
      vfill
      box0
      vfill
      }
      makeatother
      usepackage{lipsum}
      begin{document}
      fillthepage{lipsum}
      fillthepage{lipsum[1]}
      fillthepage{Very little}
      end{document}


      You can uncomment out the messages if you want to see how it decides on the font sizes to use. It doesn't make any effort to constrain horizontal size, so if you don't give it enough text to work with, you'll run off the right side of the page. (Try fillthepage{X} for an example of that.)



      It's pretty much essential that you use a vector font, otherwise it'll tend to go back and forth between two font sizes, spewing warnings and probably never converge. That's why I have the usepackage{lmodern}.



      I'm sure there are (many) cases where this doesn't work, but it was sort of fun to write.



      Edit: Maybe that protected@edef should just be a def. I was originally thinking expanding it would be better, but now I'm not so sure






      share|improve this answer





















      • 1





        +1, TH. It doesn't happen every day but today sure was one of them. Congrats, your answer left me smiling from ear to ear. :))

        – Geoffrey Jones
        Aug 24 '10 at 7:25











      • Thanks. =) The best part is, someone who's actually good at TeX will point out the trivial solution or package that someone has already written to handle this sort of thing.

        – TH.
        Aug 24 '10 at 7:30











      • Thanks so much, TH! Your code worked perfectly. I'm very new to tex and there's no way I would have been able to piece something like that together myself. :)

        – hypatia
        Aug 24 '10 at 17:33











      • This is very cool! One clarification before I open a separate question: How would I constrain it to a box of given dimensions? For the height, I can surely replace textheight with a parameter, but how do I constrain the width?

        – kongo09
        Aug 17 '11 at 16:24











      • @kongo09, If I were to rewrite this, I'd I'd replace the null vfillbox0vfill with vbox totextheight{vfillbox0 vfill}, as well as replace the protected@edef with just a def and all the 0s with z@ for that matter. And of course, you'd replace all textheight (including the one I just added in this comment) with a parameter. You can give buildbox a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.

        – TH.
        Aug 19 '11 at 4:13
















      13














      Okay, TeX isn't really designed with this in mind. That said, it can be done (sort of).



      documentclass{article}
      usepackage{lmodern}
      newdimenfontdim
      newdimenupperfontdim
      newdimenlowerfontdim
      newififmoreiterations
      fontdim12pt
      makeatletter
      defbuildbox{%
      setbox0vbox{fontsize{fontdim}{1.2fontdim}%
      selectfont
      centering
      stuff}%
      dimen@ht0
      advancedimen@dp0
      %message{Total height: thedimen@^^J}
      }

      newcommandfillthepage[1]{%
      protected@edefstuff{#1}%
      buildbox
      % Compute upper and lower bounds
      ifdimdimen@>textheight
      loop
      fontdim.5fontdim
      buildbox
      ifdimdimen@>textheight
      repeat
      lowerfontdimfontdim
      upperfontdim2fontdim
      fontdim1.5fontdim
      else
      loop
      fontdim2fontdim
      buildbox
      ifdimdimen@<textheight
      repeat
      upperfontdimfontdim
      lowerfontdim.5fontdim
      fontdim.75fontdim
      fi
      % Now try to find the optimum size
      loop
      %message{Bounds: thelowerfontdimspace
      % thefontdimspace theupperfontdim^^J}
      buildbox
      ifdimdimen@>textheight
      moreiterationstrue
      upperfontdimfontdim
      advancefontdimlowerfontdim
      fontdim.5fontdim
      else
      advancedimen@-textheight
      ifdimdimen@<10pt
      lowerfontdimfontdim
      advancefontdimupperfontdim
      fontdim.5fontdim
      dimen@upperfontdim
      advancedimen@-lowerfontdim
      ifdimdimen@<.2pt
      moreiterationsfalse
      else
      moreiterationstrue
      fi
      else
      moreiterationsfalse
      fi
      fi
      ifmoreiterations
      repeat
      % build the page
      newpage
      null
      vfill
      box0
      vfill
      }
      makeatother
      usepackage{lipsum}
      begin{document}
      fillthepage{lipsum}
      fillthepage{lipsum[1]}
      fillthepage{Very little}
      end{document}


      You can uncomment out the messages if you want to see how it decides on the font sizes to use. It doesn't make any effort to constrain horizontal size, so if you don't give it enough text to work with, you'll run off the right side of the page. (Try fillthepage{X} for an example of that.)



      It's pretty much essential that you use a vector font, otherwise it'll tend to go back and forth between two font sizes, spewing warnings and probably never converge. That's why I have the usepackage{lmodern}.



      I'm sure there are (many) cases where this doesn't work, but it was sort of fun to write.



      Edit: Maybe that protected@edef should just be a def. I was originally thinking expanding it would be better, but now I'm not so sure






      share|improve this answer





















      • 1





        +1, TH. It doesn't happen every day but today sure was one of them. Congrats, your answer left me smiling from ear to ear. :))

        – Geoffrey Jones
        Aug 24 '10 at 7:25











      • Thanks. =) The best part is, someone who's actually good at TeX will point out the trivial solution or package that someone has already written to handle this sort of thing.

        – TH.
        Aug 24 '10 at 7:30











      • Thanks so much, TH! Your code worked perfectly. I'm very new to tex and there's no way I would have been able to piece something like that together myself. :)

        – hypatia
        Aug 24 '10 at 17:33











      • This is very cool! One clarification before I open a separate question: How would I constrain it to a box of given dimensions? For the height, I can surely replace textheight with a parameter, but how do I constrain the width?

        – kongo09
        Aug 17 '11 at 16:24











      • @kongo09, If I were to rewrite this, I'd I'd replace the null vfillbox0vfill with vbox totextheight{vfillbox0 vfill}, as well as replace the protected@edef with just a def and all the 0s with z@ for that matter. And of course, you'd replace all textheight (including the one I just added in this comment) with a parameter. You can give buildbox a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.

        – TH.
        Aug 19 '11 at 4:13














      13












      13








      13







      Okay, TeX isn't really designed with this in mind. That said, it can be done (sort of).



      documentclass{article}
      usepackage{lmodern}
      newdimenfontdim
      newdimenupperfontdim
      newdimenlowerfontdim
      newififmoreiterations
      fontdim12pt
      makeatletter
      defbuildbox{%
      setbox0vbox{fontsize{fontdim}{1.2fontdim}%
      selectfont
      centering
      stuff}%
      dimen@ht0
      advancedimen@dp0
      %message{Total height: thedimen@^^J}
      }

      newcommandfillthepage[1]{%
      protected@edefstuff{#1}%
      buildbox
      % Compute upper and lower bounds
      ifdimdimen@>textheight
      loop
      fontdim.5fontdim
      buildbox
      ifdimdimen@>textheight
      repeat
      lowerfontdimfontdim
      upperfontdim2fontdim
      fontdim1.5fontdim
      else
      loop
      fontdim2fontdim
      buildbox
      ifdimdimen@<textheight
      repeat
      upperfontdimfontdim
      lowerfontdim.5fontdim
      fontdim.75fontdim
      fi
      % Now try to find the optimum size
      loop
      %message{Bounds: thelowerfontdimspace
      % thefontdimspace theupperfontdim^^J}
      buildbox
      ifdimdimen@>textheight
      moreiterationstrue
      upperfontdimfontdim
      advancefontdimlowerfontdim
      fontdim.5fontdim
      else
      advancedimen@-textheight
      ifdimdimen@<10pt
      lowerfontdimfontdim
      advancefontdimupperfontdim
      fontdim.5fontdim
      dimen@upperfontdim
      advancedimen@-lowerfontdim
      ifdimdimen@<.2pt
      moreiterationsfalse
      else
      moreiterationstrue
      fi
      else
      moreiterationsfalse
      fi
      fi
      ifmoreiterations
      repeat
      % build the page
      newpage
      null
      vfill
      box0
      vfill
      }
      makeatother
      usepackage{lipsum}
      begin{document}
      fillthepage{lipsum}
      fillthepage{lipsum[1]}
      fillthepage{Very little}
      end{document}


      You can uncomment out the messages if you want to see how it decides on the font sizes to use. It doesn't make any effort to constrain horizontal size, so if you don't give it enough text to work with, you'll run off the right side of the page. (Try fillthepage{X} for an example of that.)



      It's pretty much essential that you use a vector font, otherwise it'll tend to go back and forth between two font sizes, spewing warnings and probably never converge. That's why I have the usepackage{lmodern}.



      I'm sure there are (many) cases where this doesn't work, but it was sort of fun to write.



      Edit: Maybe that protected@edef should just be a def. I was originally thinking expanding it would be better, but now I'm not so sure






      share|improve this answer















      Okay, TeX isn't really designed with this in mind. That said, it can be done (sort of).



      documentclass{article}
      usepackage{lmodern}
      newdimenfontdim
      newdimenupperfontdim
      newdimenlowerfontdim
      newififmoreiterations
      fontdim12pt
      makeatletter
      defbuildbox{%
      setbox0vbox{fontsize{fontdim}{1.2fontdim}%
      selectfont
      centering
      stuff}%
      dimen@ht0
      advancedimen@dp0
      %message{Total height: thedimen@^^J}
      }

      newcommandfillthepage[1]{%
      protected@edefstuff{#1}%
      buildbox
      % Compute upper and lower bounds
      ifdimdimen@>textheight
      loop
      fontdim.5fontdim
      buildbox
      ifdimdimen@>textheight
      repeat
      lowerfontdimfontdim
      upperfontdim2fontdim
      fontdim1.5fontdim
      else
      loop
      fontdim2fontdim
      buildbox
      ifdimdimen@<textheight
      repeat
      upperfontdimfontdim
      lowerfontdim.5fontdim
      fontdim.75fontdim
      fi
      % Now try to find the optimum size
      loop
      %message{Bounds: thelowerfontdimspace
      % thefontdimspace theupperfontdim^^J}
      buildbox
      ifdimdimen@>textheight
      moreiterationstrue
      upperfontdimfontdim
      advancefontdimlowerfontdim
      fontdim.5fontdim
      else
      advancedimen@-textheight
      ifdimdimen@<10pt
      lowerfontdimfontdim
      advancefontdimupperfontdim
      fontdim.5fontdim
      dimen@upperfontdim
      advancedimen@-lowerfontdim
      ifdimdimen@<.2pt
      moreiterationsfalse
      else
      moreiterationstrue
      fi
      else
      moreiterationsfalse
      fi
      fi
      ifmoreiterations
      repeat
      % build the page
      newpage
      null
      vfill
      box0
      vfill
      }
      makeatother
      usepackage{lipsum}
      begin{document}
      fillthepage{lipsum}
      fillthepage{lipsum[1]}
      fillthepage{Very little}
      end{document}


      You can uncomment out the messages if you want to see how it decides on the font sizes to use. It doesn't make any effort to constrain horizontal size, so if you don't give it enough text to work with, you'll run off the right side of the page. (Try fillthepage{X} for an example of that.)



      It's pretty much essential that you use a vector font, otherwise it'll tend to go back and forth between two font sizes, spewing warnings and probably never converge. That's why I have the usepackage{lmodern}.



      I'm sure there are (many) cases where this doesn't work, but it was sort of fun to write.



      Edit: Maybe that protected@edef should just be a def. I was originally thinking expanding it would be better, but now I'm not so sure







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Aug 24 '10 at 9:36

























      answered Aug 24 '10 at 7:16









      TH.TH.

      47.7k10130197




      47.7k10130197








      • 1





        +1, TH. It doesn't happen every day but today sure was one of them. Congrats, your answer left me smiling from ear to ear. :))

        – Geoffrey Jones
        Aug 24 '10 at 7:25











      • Thanks. =) The best part is, someone who's actually good at TeX will point out the trivial solution or package that someone has already written to handle this sort of thing.

        – TH.
        Aug 24 '10 at 7:30











      • Thanks so much, TH! Your code worked perfectly. I'm very new to tex and there's no way I would have been able to piece something like that together myself. :)

        – hypatia
        Aug 24 '10 at 17:33











      • This is very cool! One clarification before I open a separate question: How would I constrain it to a box of given dimensions? For the height, I can surely replace textheight with a parameter, but how do I constrain the width?

        – kongo09
        Aug 17 '11 at 16:24











      • @kongo09, If I were to rewrite this, I'd I'd replace the null vfillbox0vfill with vbox totextheight{vfillbox0 vfill}, as well as replace the protected@edef with just a def and all the 0s with z@ for that matter. And of course, you'd replace all textheight (including the one I just added in this comment) with a parameter. You can give buildbox a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.

        – TH.
        Aug 19 '11 at 4:13














      • 1





        +1, TH. It doesn't happen every day but today sure was one of them. Congrats, your answer left me smiling from ear to ear. :))

        – Geoffrey Jones
        Aug 24 '10 at 7:25











      • Thanks. =) The best part is, someone who's actually good at TeX will point out the trivial solution or package that someone has already written to handle this sort of thing.

        – TH.
        Aug 24 '10 at 7:30











      • Thanks so much, TH! Your code worked perfectly. I'm very new to tex and there's no way I would have been able to piece something like that together myself. :)

        – hypatia
        Aug 24 '10 at 17:33











      • This is very cool! One clarification before I open a separate question: How would I constrain it to a box of given dimensions? For the height, I can surely replace textheight with a parameter, but how do I constrain the width?

        – kongo09
        Aug 17 '11 at 16:24











      • @kongo09, If I were to rewrite this, I'd I'd replace the null vfillbox0vfill with vbox totextheight{vfillbox0 vfill}, as well as replace the protected@edef with just a def and all the 0s with z@ for that matter. And of course, you'd replace all textheight (including the one I just added in this comment) with a parameter. You can give buildbox a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.

        – TH.
        Aug 19 '11 at 4:13








      1




      1





      +1, TH. It doesn't happen every day but today sure was one of them. Congrats, your answer left me smiling from ear to ear. :))

      – Geoffrey Jones
      Aug 24 '10 at 7:25





      +1, TH. It doesn't happen every day but today sure was one of them. Congrats, your answer left me smiling from ear to ear. :))

      – Geoffrey Jones
      Aug 24 '10 at 7:25













      Thanks. =) The best part is, someone who's actually good at TeX will point out the trivial solution or package that someone has already written to handle this sort of thing.

      – TH.
      Aug 24 '10 at 7:30





      Thanks. =) The best part is, someone who's actually good at TeX will point out the trivial solution or package that someone has already written to handle this sort of thing.

      – TH.
      Aug 24 '10 at 7:30













      Thanks so much, TH! Your code worked perfectly. I'm very new to tex and there's no way I would have been able to piece something like that together myself. :)

      – hypatia
      Aug 24 '10 at 17:33





      Thanks so much, TH! Your code worked perfectly. I'm very new to tex and there's no way I would have been able to piece something like that together myself. :)

      – hypatia
      Aug 24 '10 at 17:33













      This is very cool! One clarification before I open a separate question: How would I constrain it to a box of given dimensions? For the height, I can surely replace textheight with a parameter, but how do I constrain the width?

      – kongo09
      Aug 17 '11 at 16:24





      This is very cool! One clarification before I open a separate question: How would I constrain it to a box of given dimensions? For the height, I can surely replace textheight with a parameter, but how do I constrain the width?

      – kongo09
      Aug 17 '11 at 16:24













      @kongo09, If I were to rewrite this, I'd I'd replace the null vfillbox0vfill with vbox totextheight{vfillbox0 vfill}, as well as replace the protected@edef with just a def and all the 0s with z@ for that matter. And of course, you'd replace all textheight (including the one I just added in this comment) with a parameter. You can give buildbox a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.

      – TH.
      Aug 19 '11 at 4:13





      @kongo09, If I were to rewrite this, I'd I'd replace the null vfillbox0vfill with vbox totextheight{vfillbox0 vfill}, as well as replace the protected@edef with just a def and all the 0s with z@ for that matter. And of course, you'd replace all textheight (including the one I just added in this comment) with a parameter. You can give buildbox a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.

      – TH.
      Aug 19 '11 at 4:13











      1














      I'm not quite sure what you are actually trying to do. But "but minipage overrode the vfill spacing" sounds as if you have put the vfill inside the minipage. This makes only sense if you give the minipage a specific height (which is possible with an optional argument). If you want to move the minipage itself the vfill must go outside.



      You can use varwidth to get a minipage which adjust its width to its content. You can use resizebox to scale a minipage to a given width.



      Btw: Center things horizontally is in general a well-defined conzept: Chars, boxes and words have only one width and the reference point one wants to use is clear. This is different when you want to center things vertically. Chars and boxes have height and depth. Do you want to center a "A" along the baseline or along a line somewhere in the middle of the char? You can see the problem if you remove the [b] in the last example.



      documentclass{article}
      usepackage{varwidth,lipsum,graphics}
      begin{document}
      vspace*{fill}

      {centering
      begin{minipage}{0.5textwidth}
      lipsum[1]
      end{minipage}
      par}


      vfill

      newpage
      vspace*{fill}

      {centering
      begin{varwidth}{0.5textwidth}
      abc
      end{varwidth}
      par}


      vfill
      newpage
      vspace*{fill}

      {centering
      begin{minipage}{0.5textwidth}
      centering abc\cdeaddff
      end{minipage}
      par}


      vfill

      newpage
      vspace*{fill}

      {centering
      resizebox{textwidth}{!}{%
      .begin{varwidth}[b]{0.5textwidth}
      abc
      end{varwidth}}
      par}
      vfill
      end{document}





      share|improve this answer




























        1














        I'm not quite sure what you are actually trying to do. But "but minipage overrode the vfill spacing" sounds as if you have put the vfill inside the minipage. This makes only sense if you give the minipage a specific height (which is possible with an optional argument). If you want to move the minipage itself the vfill must go outside.



        You can use varwidth to get a minipage which adjust its width to its content. You can use resizebox to scale a minipage to a given width.



        Btw: Center things horizontally is in general a well-defined conzept: Chars, boxes and words have only one width and the reference point one wants to use is clear. This is different when you want to center things vertically. Chars and boxes have height and depth. Do you want to center a "A" along the baseline or along a line somewhere in the middle of the char? You can see the problem if you remove the [b] in the last example.



        documentclass{article}
        usepackage{varwidth,lipsum,graphics}
        begin{document}
        vspace*{fill}

        {centering
        begin{minipage}{0.5textwidth}
        lipsum[1]
        end{minipage}
        par}


        vfill

        newpage
        vspace*{fill}

        {centering
        begin{varwidth}{0.5textwidth}
        abc
        end{varwidth}
        par}


        vfill
        newpage
        vspace*{fill}

        {centering
        begin{minipage}{0.5textwidth}
        centering abc\cdeaddff
        end{minipage}
        par}


        vfill

        newpage
        vspace*{fill}

        {centering
        resizebox{textwidth}{!}{%
        .begin{varwidth}[b]{0.5textwidth}
        abc
        end{varwidth}}
        par}
        vfill
        end{document}





        share|improve this answer


























          1












          1








          1







          I'm not quite sure what you are actually trying to do. But "but minipage overrode the vfill spacing" sounds as if you have put the vfill inside the minipage. This makes only sense if you give the minipage a specific height (which is possible with an optional argument). If you want to move the minipage itself the vfill must go outside.



          You can use varwidth to get a minipage which adjust its width to its content. You can use resizebox to scale a minipage to a given width.



          Btw: Center things horizontally is in general a well-defined conzept: Chars, boxes and words have only one width and the reference point one wants to use is clear. This is different when you want to center things vertically. Chars and boxes have height and depth. Do you want to center a "A" along the baseline or along a line somewhere in the middle of the char? You can see the problem if you remove the [b] in the last example.



          documentclass{article}
          usepackage{varwidth,lipsum,graphics}
          begin{document}
          vspace*{fill}

          {centering
          begin{minipage}{0.5textwidth}
          lipsum[1]
          end{minipage}
          par}


          vfill

          newpage
          vspace*{fill}

          {centering
          begin{varwidth}{0.5textwidth}
          abc
          end{varwidth}
          par}


          vfill
          newpage
          vspace*{fill}

          {centering
          begin{minipage}{0.5textwidth}
          centering abc\cdeaddff
          end{minipage}
          par}


          vfill

          newpage
          vspace*{fill}

          {centering
          resizebox{textwidth}{!}{%
          .begin{varwidth}[b]{0.5textwidth}
          abc
          end{varwidth}}
          par}
          vfill
          end{document}





          share|improve this answer













          I'm not quite sure what you are actually trying to do. But "but minipage overrode the vfill spacing" sounds as if you have put the vfill inside the minipage. This makes only sense if you give the minipage a specific height (which is possible with an optional argument). If you want to move the minipage itself the vfill must go outside.



          You can use varwidth to get a minipage which adjust its width to its content. You can use resizebox to scale a minipage to a given width.



          Btw: Center things horizontally is in general a well-defined conzept: Chars, boxes and words have only one width and the reference point one wants to use is clear. This is different when you want to center things vertically. Chars and boxes have height and depth. Do you want to center a "A" along the baseline or along a line somewhere in the middle of the char? You can see the problem if you remove the [b] in the last example.



          documentclass{article}
          usepackage{varwidth,lipsum,graphics}
          begin{document}
          vspace*{fill}

          {centering
          begin{minipage}{0.5textwidth}
          lipsum[1]
          end{minipage}
          par}


          vfill

          newpage
          vspace*{fill}

          {centering
          begin{varwidth}{0.5textwidth}
          abc
          end{varwidth}
          par}


          vfill
          newpage
          vspace*{fill}

          {centering
          begin{minipage}{0.5textwidth}
          centering abc\cdeaddff
          end{minipage}
          par}


          vfill

          newpage
          vspace*{fill}

          {centering
          resizebox{textwidth}{!}{%
          .begin{varwidth}[b]{0.5textwidth}
          abc
          end{varwidth}}
          par}
          vfill
          end{document}






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 24 '10 at 11:56









          Ulrike FischerUlrike Fischer

          192k8302683




          192k8302683























              1














              fitting library from tcolorbox allows to adjust a given text to some width and height. Default font size is never enlarged, but you can start with a large font size (fit basedim) which will be reduced until all text fits in desired box.



              documentclass{article}
              usepackage{lmodern}
              usepackage[most]{tcolorbox}
              usepackage{lipsum}

              tcbset{valign=center, halign=center}

              begin{document}
              begin{tcolorbox}[fit to height=.4textheight, fit basedim=50pt]
              lipsum[1]
              end{tcolorbox}

              begin{tcolorbox}[fit to height=.4textheight, fit basedim=50pt]
              lipsum[1-5]
              end{tcolorbox}
              end{document}


              enter image description here






              share|improve this answer




























                1














                fitting library from tcolorbox allows to adjust a given text to some width and height. Default font size is never enlarged, but you can start with a large font size (fit basedim) which will be reduced until all text fits in desired box.



                documentclass{article}
                usepackage{lmodern}
                usepackage[most]{tcolorbox}
                usepackage{lipsum}

                tcbset{valign=center, halign=center}

                begin{document}
                begin{tcolorbox}[fit to height=.4textheight, fit basedim=50pt]
                lipsum[1]
                end{tcolorbox}

                begin{tcolorbox}[fit to height=.4textheight, fit basedim=50pt]
                lipsum[1-5]
                end{tcolorbox}
                end{document}


                enter image description here






                share|improve this answer


























                  1












                  1








                  1







                  fitting library from tcolorbox allows to adjust a given text to some width and height. Default font size is never enlarged, but you can start with a large font size (fit basedim) which will be reduced until all text fits in desired box.



                  documentclass{article}
                  usepackage{lmodern}
                  usepackage[most]{tcolorbox}
                  usepackage{lipsum}

                  tcbset{valign=center, halign=center}

                  begin{document}
                  begin{tcolorbox}[fit to height=.4textheight, fit basedim=50pt]
                  lipsum[1]
                  end{tcolorbox}

                  begin{tcolorbox}[fit to height=.4textheight, fit basedim=50pt]
                  lipsum[1-5]
                  end{tcolorbox}
                  end{document}


                  enter image description here






                  share|improve this answer













                  fitting library from tcolorbox allows to adjust a given text to some width and height. Default font size is never enlarged, but you can start with a large font size (fit basedim) which will be reduced until all text fits in desired box.



                  documentclass{article}
                  usepackage{lmodern}
                  usepackage[most]{tcolorbox}
                  usepackage{lipsum}

                  tcbset{valign=center, halign=center}

                  begin{document}
                  begin{tcolorbox}[fit to height=.4textheight, fit basedim=50pt]
                  lipsum[1]
                  end{tcolorbox}

                  begin{tcolorbox}[fit to height=.4textheight, fit basedim=50pt]
                  lipsum[1-5]
                  end{tcolorbox}
                  end{document}


                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 12 at 19:08









                  IgnasiIgnasi

                  93.5k4169311




                  93.5k4169311






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f2231%2ffitting-and-centering-text-both-in-a-constrained-area%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents