如果我能写出类似这样的内容,那对我来说会非常方便:
\phantomwrapper{long word or text that fits in one line}{shorter text}
phantomwrapper 会在其第二个参数周围创建恰好数量的空白,因此结果需要的水平空间与第一个参数一样多。一个示例应用程序是 tikz 节点,我想用 fit 在其周围绘制框,并且我希望让所有框保持相同的大小。
答案1
这个eqparbox
包可能就是您想要的:它定义了常用框命令的变体,eqparbox
,eqmakebox
,eqframebox
,它们接受标签而不是宽度参数。所有具有相同标签的框都将具有最长文本的宽度,并且eqboxwidth{tag}
定义了长度,可用于定义另一个框的宽度。
如果要定义width and height
其他内容的框,我会xmakebox
基于非常小的文本宽度和高度来定义一个宏makebox
。当然,它假设较短文本的宽度和高度确实小于较长文本的宽度和高度。请注意,较短文本不一定是单行文本。这是一个简短的演示:
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage{makebox}
\usepackage{eqparbox}
\setlength\fboxsep{12pt}
\newcommand\xmakebox[2]{\makebox*{#1}{\vphantom{#1}#2}}
\begin{document}
\begin{tabular}{c}
\eqframebox[boxa]{Blahblah}\\\\
\eqframebox[boxa]{Blahblahblah, blahblahblah, blahblahblah…}\\\\
\fbox{\parbox{\eqboxwidth{boxa}}{\lipsum[2]}}
\end{tabular}
\vskip 0.5cm
\setlength{\fboxsep}{0pt}
\noindent\fbox{\makebox*{Blahblahblah, blahblahblah, blahblahblah…}{\begin{tabular}{|l|}Is that OK?\\Is that OK?\\Is that OK?
\end{tabular}}}\\[0.5cm]
\fbox{Blahblahblah, blahblahblah, blahblahblah…}\\[0.5cm]
\fbox{\xmakebox{\begin{tabular}{@{}l@{}}Blahblahblah, blahblahblah, blahblahblah…\\Is that OK?\\Is that OK?\\Is that OK?\\Is that OK?
\end{tabular}}{Blahblahblah}}\qquad\begin{tabular}{@{}l@{}}Blahblahblah, blahblahblah, blahblahblah…\\Is that OK?\\Is that OK?\\Is that OK?\\Is that OK?
\end{tabular}
\end{document}
添加:它似乎为@Mico 的第二个问题提供了部分解决方案:如果当前行剩余空间不够,您可以通过以下方式减少框的宽度,例如:\parbox{0.5\eqboxwidth{tag}}{…}
。
答案2
如果你不需要处理换行符,那么它只是一个calc
包的应用程序:
\documentclass{article}
\usepackage{calc}
\newcommand{\printtowidth}[2]{%
\makebox[\widthof{#1}]{#2}%
}
\begin{document}
Some words before abc def ghi and some words after.
Some words before \printtowidth{abc def ghi}{xyz} and some words after.
\end{document}
换行符会带来在何处打印内部文本的问题。