如何將一行文字縮放到精確的寬度

如何將一行文字縮放到精確的寬度

我想縮放一些文字行,以便它們在左側和右側都完美排列。使用 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}

它產生如下輸出:

使用 resizebox 的線條排列得不太完美

答案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}

在此輸入影像描述

相關內容