如何使网格中的字符水平对齐?

如何使网格中的字符水平对齐?

我有一个 LaTeX 脚本,用于在网格内渲染字符。虽然它的功能足够好,但有一个小问题,即如果某些字符占用的空间太小,字符的对齐就会被打乱。

% !TeX program = xelatex
\documentclass{article}
\usepackage{xeCJK}
\usepackage{tikz}
\usepackage[outline]{contour}
\usepackage{graphicx}
\graphicspath{ {./} }
\setCJKmainfont{Kaiti SC}
% \setCJKmainfont{Xingkai SC}
% \setCJKmainfont{STKaiti}

\newcommand\Grid[1]{%
 \tikz[baseline=(char.base)]{%
  \draw[xstep=1ex,ystep=1ex,help lines] (-1ex,-1ex) grid (1ex,1ex);
  \draw[help lines,densely dash dot]
  (-1ex,-1ex) -- (1ex,1ex)  (-1ex,1ex) -- (1ex,-1ex);
  \node[inner sep=0pt] (char) at (0,0) {#1};
 }%
}
\pagenumbering{gobble}
\begin{document}
\begin{center}
    \Huge 芙蓉楼送辛渐 \\
\end{center}
\begin{flushright}
    \huge 唐 $\bullet$ 王昌龄 \\
\end{flushright}
\begin{center}
    \Huge 寒雨连江夜入吴 \\
    \Huge 平明送客楚山孤 \\
    \Huge 洛阳亲友如相问 \\
    \Huge 一片冰心在玉壶 \\
\end{center}
\hrule
\begin{center}
    \Huge \Grid{芙}\Grid{蓉}\Grid{楼}\Grid{送}\Grid{辛}\Grid{渐} \\
\end{center}
\begin{flushright}
    \huge \Grid{唐} $\bullet$ \Grid{王}\Grid{昌}\Grid{龄} \\
\end{flushright}
\begin{center}
    \Huge \Grid{寒}\Grid{雨}\Grid{连}\Grid{江}\Grid{夜}\Grid{入}\Grid{吴} \\
    \Huge \Grid{平}\Grid{明}\Grid{送}\Grid{客}\Grid{楚}\Grid{山}\Grid{孤} \\
    \Huge \Grid{洛}\Grid{阳}\Grid{亲}\Grid{友}\Grid{如}\Grid{相}\Grid{问} \\
    \Huge \Grid{一}\Grid{片}\Grid{冰}\Grid{心}\Grid{在}\Grid{玉}\Grid{壶} \\
\end{center}
% \hrule
% \newpage
% \begin{figure}
%     \centering
%     \makebox[0pt]{\includegraphics[width=0.9\paperwidth]{image.jpg}}
% \end{figure}
\end{document}

芙蓉楼送辛渐

看看放大的那个,它破坏了对齐,这个问题可能是因为放大的字符占用的空间较少而发生的。为了解决这个问题,我们如何应用对齐来确保字符的一致定位?

答案1

您需要为anchor包含该字符的节点设置高度,或者确保该节点具有相同的高度,而不管其内容如何。您可能需要垂直移动节点一点,以使其与网格正确对齐。

对于不同的解决方案:

% !TeX program = xelatex
\documentclass{article}
\usepackage{xeCJK}
\usepackage{tikz}
\setCJKmainfont{FandolKai}

\newcommand\Grid[1]{%
 \tikz[baseline=(char.base)]{%
  \draw[xstep=1ex,ystep=1ex,help lines] (-1ex,-1ex) grid (1ex,1ex);
  \draw[help lines,densely dash dot]
  (-1ex,-1ex) -- (1ex,1ex)  (-1ex,1ex) -- (1ex,-1ex);
  \node[inner sep=0pt,anchor=base,yshift=-0.7ex] (char) at (0,0) {#1};
 }%
}

\pagenumbering{gobble}

\begin{document}

\begin{center}
    \Huge \Grid{芙}\Grid{蓉}\Grid{楼}\Grid{送}\Grid{辛}\Grid{渐} \\
\end{center}
\begin{flushright}
    \huge \Grid{唐} $\bullet$ \Grid{王}\Grid{昌}\Grid{龄} \\
\end{flushright}
\begin{center}
    \Huge \Grid{寒}\Grid{雨}\Grid{连}\Grid{江}\Grid{夜}\Grid{入}\Grid{吴} \\
    \Huge \Grid{平}\Grid{明}\Grid{送}\Grid{客}\Grid{楚}\Grid{山}\Grid{孤} \\
    \Huge \Grid{洛}\Grid{阳}\Grid{亲}\Grid{友}\Grid{如}\Grid{相}\Grid{问} \\
    \Huge \Grid{一}\Grid{片}\Grid{冰}\Grid{心}\Grid{在}\Grid{玉}\Grid{壶} \\
\end{center}

\end{document}

% !TeX program = xelatex
\documentclass{article}
\usepackage{xeCJK}
\usepackage{tikz}
\setCJKmainfont{FandolKai}

\newcommand\Grid[1]{%
 \tikz[baseline=(char.base)]{%
  \draw[xstep=1ex,ystep=1ex,help lines] (-1ex,-1ex) grid (1ex,1ex);
  \draw[help lines,densely dash dot]
  (-1ex,-1ex) -- (1ex,1ex)  (-1ex,1ex) -- (1ex,-1ex);
  \node[inner sep=0pt,yshift=-0.15ex] (char) at (0,0) {#1\strut};
 }%
}
\pagenumbering{gobble}

\begin{document}

\begin{center}
    \Huge \Grid{芙}\Grid{蓉}\Grid{楼}\Grid{送}\Grid{辛}\Grid{渐} \\
\end{center}
\begin{flushright}
    \huge \Grid{唐} $\bullet$ \Grid{王}\Grid{昌}\Grid{龄} \\
\end{flushright}
\begin{center}
    \Huge \Grid{寒}\Grid{雨}\Grid{连}\Grid{江}\Grid{夜}\Grid{入}\Grid{吴} \\
    \Huge \Grid{平}\Grid{明}\Grid{送}\Grid{客}\Grid{楚}\Grid{山}\Grid{孤} \\
    \Huge \Grid{洛}\Grid{阳}\Grid{亲}\Grid{友}\Grid{如}\Grid{相}\Grid{问} \\
    \Huge \Grid{一}\Grid{片}\Grid{冰}\Grid{心}\Grid{在}\Grid{玉}\Grid{壶} \\
\end{center}

\end{document}

两种解决方案的输出:

在此处输入图片描述

相关内容