Fitting and centering text (both!) in a constrained area
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
add a comment |
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
What is the point of theminipage
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
add a comment |
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
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
minipage
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 theminipage
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
add a comment |
What is the point of theminipage
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
add a comment |
3 Answers
3
active
oldest
votes
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 message
s 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
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 replacetextheight
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 thenull vfillbox0vfill
withvbox totextheight{vfillbox0 vfill}
, as well as replace theprotected@edef
with just adef
and all the0
s withz@
for that matter. And of course, you'd replace alltextheight
(including the one I just added in this comment) with a parameter. You can givebuildbox
a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.
– TH.
Aug 19 '11 at 4:13
|
show 1 more comment
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}
add a comment |
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}
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 message
s 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
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 replacetextheight
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 thenull vfillbox0vfill
withvbox totextheight{vfillbox0 vfill}
, as well as replace theprotected@edef
with just adef
and all the0
s withz@
for that matter. And of course, you'd replace alltextheight
(including the one I just added in this comment) with a parameter. You can givebuildbox
a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.
– TH.
Aug 19 '11 at 4:13
|
show 1 more comment
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 message
s 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
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 replacetextheight
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 thenull vfillbox0vfill
withvbox totextheight{vfillbox0 vfill}
, as well as replace theprotected@edef
with just adef
and all the0
s withz@
for that matter. And of course, you'd replace alltextheight
(including the one I just added in this comment) with a parameter. You can givebuildbox
a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.
– TH.
Aug 19 '11 at 4:13
|
show 1 more comment
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 message
s 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
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 message
s 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
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 replacetextheight
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 thenull vfillbox0vfill
withvbox totextheight{vfillbox0 vfill}
, as well as replace theprotected@edef
with just adef
and all the0
s withz@
for that matter. And of course, you'd replace alltextheight
(including the one I just added in this comment) with a parameter. You can givebuildbox
a parameter that is the width and replace its box with setbox0vbox{hsize=#1 fontsize...}`.
– TH.
Aug 19 '11 at 4:13
|
show 1 more comment
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 replacetextheight
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 thenull vfillbox0vfill
withvbox totextheight{vfillbox0 vfill}
, as well as replace theprotected@edef
with just adef
and all the0
s withz@
for that matter. And of course, you'd replace alltextheight
(including the one I just added in this comment) with a parameter. You can givebuildbox
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 0
s 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 0
s 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
|
show 1 more comment
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}
add a comment |
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}
add a comment |
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}
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}
answered Aug 24 '10 at 11:56
Ulrike FischerUlrike Fischer
192k8302683
192k8302683
add a comment |
add a comment |
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}
add a comment |
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}
add a comment |
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}
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}
answered Feb 12 at 19:08
IgnasiIgnasi
93.5k4169311
93.5k4169311
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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