我有以下代码产生以下结果
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\usepackage{enumitem}
\newcommand{\hour}[1]{\tikz{
\node[draw, rounded corners=1mm,text depth=0.8ex,line width=1pt] (a) {\Large{}#1}}}
\begin{document}
\begin{itemize}[label=$\bullet$]
\item \hour{Hour}
\end{itemize}
\end{document}
我想:
- 将文本置于框内中央
- 将框的中心与项目符号对齐
我怎样才能实现这个目标?
答案1
我会这么做:
- 使用节点 (a) 设置基线。这样,您将获得与正常大小文本正确的对齐方式。如果您希望大文本有更好的对齐方式,您也可以更改项目符号的大小。
\strut
在节点中放置(或不放置)。使用 时\strut
,所有框的高度都相同。不使用 时,文本将垂直居中。
\documentclass{article}
\usepackage {enumitem}
\usepackage {tikz}
\newcommand{\firsthour}[1]{%
\tikz[baseline=(a.base)]
\node[draw,rounded corners=1mm,line width=1pt,inner sep=0.3ex] (a) {\Large\strut#1};% <-- We don't want a space here
}
\newcommand{\secondhour}[1]{%
\tikz[baseline=(a.base)]
\node[draw,rounded corners=1mm,line width=1pt,inner sep=0.7ex] (a) {\Large#1};% <-- We don't want a space here
}
\begin{document}
\begin{itemize}[label=$\bullet$]
\item \firsthour{Hour} is aligned (w.r.t. normal size text),
\item \firsthour{Hour} is not centered, but
\item \firsthour{3 p.m.} is centered.
\end{itemize}
\bigskip
\begin{itemize}[label=\Large$\bullet$]
\item \firsthour{Hour} is aligned (w.r.t. large size text),
\item \firsthour{Hour} is not centered, but
\item \firsthour{3 p.m.} is centered.
\end{itemize}
\bigskip
\begin{itemize}[label=$\bullet$]
\item \secondhour{Hour} is aligned (w.r.t. normal size text),
\item \secondhour{Hour} is centered, and
\item \secondhour{3 p.m.} is centered.
\end{itemize}
\end{document}
答案2
对节点的基线位置进行一些手动调整:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\usepackage{enumitem}
\newcommand{\hour}[1]{\tikz[baseline=-0.5ex]{
\node[draw, rounded corners=1mm, very thick,
text depth=0.25ex,
font=\Large] (a) {#1}}
}
\begin{document}
\begin{itemize}[label=$\bullet$]
\item \hour{Hour}
\end{itemize}
\end{document}