我想缩放一些文本行,使它们在左侧和右侧完美对齐。使用 resizebox* 几乎可以正常工作,但边缘处有一点空白。字体越大,空白越大。
\documentclass{article}
\usepackage{graphicx}
\setkeys{Gin}{keepaspectratio} % to maintain aspect ratio of content inside resizebox
\setlength{\fboxsep}{0cm} % So that we can see the exact box around some text
\begin{document}%
\setlength{\parindent}{0cm}
\fbox{\resizebox*{12cm}{2cm}{OOOOOOOOOOOOOOOOOOOOOO}}\\%
\fbox{\resizebox*{12cm}{2cm}{OOOOOOOOOOOOOO}}\\%
\fbox{\resizebox*{12cm}{2cm}{OOOOOO}}\\%
\end{document}
它产生如下输出:
答案1
如果您愿意使用 XeLaTeX,您可以毫不费力地做到这一点。
\documentclass{article}
\usepackage{fontspec}
\usepackage{graphicx}
\newcommand{\removeleft}[1]{%
\leavevmode\kern-\XeTeXglyphbounds1 \the\XeTeXcharglyph`#1\relax
#1%
}
\newcommand{\removeright}[1]{%
#1%
\kern-\XeTeXglyphbounds3 \the\XeTeXcharglyph`#1\relax
}
\setlength{\parindent}{0pt}
\setlength{\fboxsep}{0pt}
\begin{document}
\fbox{\resizebox{12cm}{!}{\removeleft{O}OOOOOOOOOOOOOOOOOOOO\removeright{O}}}
\fbox{\resizebox{12cm}{!}{\removeleft{O}OOOOOOOOOOOO\removeright{O}}}
\fbox{\resizebox{12cm}{!}{\removeleft{O}OOOO\removeright{O}}}
\end{document}
XeTeX 文档中解释了这个技巧。首先,我们获取第一个字符的字形编号,然后测量其左侧边距。最后一个字符也一样。
通过一些expl3
技巧,我们可以避免分离第一个字母和最后一个字母:
\documentclass{article}
\usepackage{fontspec}
\usepackage{graphicx}
\newcommand{\remove}[2]{%
\leavevmode\kern-\XeTeXglyphbounds#2\space\the\XeTeXcharglyph`#1\relax
}
\ExplSyntaxOn
\cs_set_eq:NN \tobyone_remove_sb:nn \remove
\cs_generate_variant:Nn \tobyone_remove_sb:nn { fn }
\NewDocumentCommand{\removesidebearings}{m}
{
\tobyone_remove_sb:fn { \tl_head:n { #1 } } { 1 }% left
#1
\tobyone_remove_sb:fn { \tl_head:f { \tl_reverse:n { #1 } } } { 3 }
}
\ExplSyntaxOff
\setlength{\parindent}{0pt}
\setlength{\fboxsep}{0pt}
\begin{document}
\fbox{\resizebox{12cm}{!}{\removesidebearings{OOOOOOOOOOOOOOOOOOOOOO}}}
\fbox{\resizebox{12cm}{!}{\removesidebearings{OOOOOOOOOOOOOO}}}
\fbox{\resizebox{12cm}{!}{\removesidebearings{OOOOOO}}}
\end{document}
如果还需要 TeX 的特殊字符,请按如下方式添加定义并使用“符号名称”。
\documentclass{article}
\usepackage{fontspec}
\usepackage{graphicx}
\newcommand{\remove}[2]{%
\leavevmode\kern-\XeTeXglyphbounds#2\space\the\XeTeXcharglyph`#1\relax
}
\ExplSyntaxOn
\cs_set_eq:NN \tobyone_remove_sb:nn \remove
\cs_generate_variant:Nn \tobyone_remove_sb:nn { fn }
\NewDocumentCommand{\removesidebearings}{m}
{
\tobyone_remove_sb:fn { \tl_head:n { #1 } } { 1 }% left
#1
\tobyone_remove_sb:fn { \tl_head:f { \tl_reverse:n { #1 } } } { 3 }
}
\cs_set_eq:NN \ampersandchar \c_ampersand_str
\cs_set_eq:NN \backslashchar \c_backslash_str
\cs_set_eq:NN \leftbracechar \c_left_brace_str
\cs_set_eq:NN \rightbracechar \c_right_brace_str
\cs_set_eq:NN \circumflexchar \c_circumflex_str
\cs_set_eq:NN \dollarchar \c_dollar_str
\cs_set_eq:NN \hashchar \c_hash_str
\cs_set_eq:NN \percentchar \c_percent_str
\cs_set_eq:NN \tildechar \c_tilde_str
\cs_set_eq:NN \underscorechar \c_underscore_str
\ExplSyntaxOff
\setlength{\parindent}{0pt}
\setlength{\fboxsep}{0pt}
\begin{document}
\fbox{\resizebox{12cm}{!}{\removesidebearings{\dollarchar abc\rightbracechar}}}
\fbox{\resizebox{12cm}{!}{\dollarchar abc\rightbracechar}}
\end{document}
答案2
这是可行的,但它基于反复试验(没有干净的解决方案):
\documentclass{article}
\usepackage{graphicx}
\setkeys{Gin}{keepaspectratio} % to maintain aspect ratio of content inside resizebox
\setlength{\fboxsep}{0cm} % So that we can see the exact box around some text
\newlength\mycor
\mycor=-0.555428pt
\begin{document}%
\setlength{\parindent}{0cm}
\fbox{\resizebox*{12cm}{2cm}{\hspace*{\mycor}OOOOOOOOOOOOOOOOOOOOOO}\hspace*{\mycor}}\\%
\fbox{\resizebox*{12cm}{2cm}{\hspace*{\mycor}OOOOOOOOOOOOOO\hspace*{\mycor}}}\\%
\fbox{\resizebox*{12cm}{2cm}{\hspace*{\mycor}OOOOOO\hspace*{\mycor}}}\\%
\end{document}