![在 TikZ 中将字母与下方线条对齐](https://linux22.com/image/342002/%E5%9C%A8%20TikZ%20%E4%B8%AD%E5%B0%86%E5%AD%97%E6%AF%8D%E4%B8%8E%E4%B8%8B%E6%96%B9%E7%BA%BF%E6%9D%A1%E5%AF%B9%E9%BD%90.png)
我正在尝试制作一张名片,并希望将路径(在本例中为线)的开头与字母完美对齐。但是,由于添加了间距前字母,对齐似乎不完美。节点上的对齐已完成(应该如此),但我想删除第一个(大写)字母之前的少量空格。
我也尝试使用\underline
,但没有成功。
MWE(我使用XeLaTeX):
% !TEX TS-program = xelatex
\documentclass{standalone}
\usepackage{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,positioning}
\begin{document}
% preferred method
\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0},framed]
\draw[anchor=west] (0,0) node[align=left] (name) {\Huge Here};
\draw[line width=0.03cm] ($(name.south west)+(0,-0.03in)$) -- ($(name.south west)+(1in,-0.03in)$);
\end{tikzpicture}
% attempt with underline
\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0},framed]
\draw[anchor=west] (0,0) node[align=left] (name) {\Huge\underline{Here}};
\draw[line width=0.03cm] ($(name.south west)+(0,-0.03in)$) -- ($(name.south west)+(1in,-0.03in)$);
\end{tikzpicture}
\end{document}
(我知道,这个 MWE 实际上不需要 XeLaTeX。)
答案1
如果使用 OpenType(/TrueType) 字体(PFB 字体不起作用),XeTeX 就会知道侧边距:
\XeTeXglyphbounds1\XeTeXglyphindex"H"
应用于问题的“首选方法”:
\documentclass{standalone}
\usepackage{standalone}
\usepackage{fontspec}% OpenType of Latin Modern fonts
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,positioning}
\begin{document}
% preferred method
\begin{tikzpicture}[
every node/.style={inner sep=0,outer sep=0},
framed,
]
\draw[anchor=west] (0,0) node[align=left] (name) {%
\Huge Here%
\xdef\LeftSideBearingH{\the\XeTeXglyphbounds1\XeTeXglyphindex"H" }%
\typeout{* Left side bearing of H: \LeftSideBearingH}%
};
\draw[line width=0.03cm]
($(name.south west)+(\LeftSideBearingH, -0.03in)$) -- ++(1in, 0);
\end{tikzpicture}
\end{document}
报告值:
* Left side bearing of H: 1.26888pt
答案2
像这样?
% !TEX TS-program = xelatex
\documentclass[border=3mm, tikz]{standalone}
\usetikzlibrary{backgrounds,calc,positioning}
\begin{document}
% preferred method
\begin{tikzpicture}[
here/.style = {font=\Huge,
inner xsep=-1.2pt,% <-- here is the trick
inner ysep=0.03in, outer sep=0pt},
framed ]
\node[here] (name) {Here};
\draw[line width=0.03cm] (name.south west) -- ($(name.south west)+(1in,0in)$);
\end{tikzpicture}
\end{document}
或考虑敲击注释,只需将行移到1.2pt
右侧(结果与之前相同):
% !TEX TS-program = xelatex
\documentclass[border=3mm, tikz]{standalone}
\usetikzlibrary{backgrounds}
\begin{document}
% preferred method
\begin{tikzpicture}[
here/.style = {font=\Huge,
inner xsep=0pt,inner ysep=0.03in, outer sep=0pt},
framed ]
\node[here] (name) {Here};
\draw[line width=0.03cm] ([xshift=1.2pt] name.south west) -- ([xshift=1in] name.south west);
\end{tikzpicture}
\end{document}