我曾使用过scale=0.5
一些节点来缩小文本,但最近代码发生了变化,我别无选择,只能寻找一种方法来保持节点的原始大小(它是固定宽度的节点类型)。我不考虑使用\tiny
或任何标准字体大小命令,因为文本看起来scale=0.5
与完全不同\tiny
(下面的代码用于确认差异)。如何才能在不影响节点绘图框的情况下缩放节点的字体?
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset
{%
ORIGINAL/.style=
{%
draw,
text width=50mm
}
}
\begin{document}
\begin{tikzpicture}
\path node[ORIGINAL](one)
{ % ORIGINAL TEXT-SIZE (MUST BE CUT IN HALF)
hello world (original)
};
\path node
[ORIGINAL,scale=0.5,below=2mm of one](two)
{ % BOTH, TEXT AND NODE ARE SCALED-DOWN (NO GOOD)
hello world (scales node too)
};
\path node
[ORIGINAL,font=\tiny,below=2mm of two ]
{ % TEXT IS SMALLER BUT NOT ENOUGH AND LOOKS WEIRD (NO GOOD)
hello world (not exactly right)
};
\end{tikzpicture}
\end{document}
答案1
像这样:
对于第二个节点中的文本,我使用带有缩放内容的标签:
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset
{%
ORIGINAL/.style=
{%
draw,
text width=50mm
}
}
\begin{document}
\begin{tikzpicture}
\node[ORIGINAL](one)
{ % ORIGINAL TEXT-SIZE (MUST BE CUT IN HALF)
hello world (original)
};
\node[ORIGINAL,below=2mm of one,
label={[scale=0.5]center:hello world (doesn't scale node size!)}] (two)
{ };
\end{tikzpicture}
\end{document}
附录。 以上新版本姆韦考虑以下您的意见:
对于这幅图像,我重新定义了节点样式。现在有两个参数:text width
和scale
。它们的值对于未缩放的节点由设置/.default
,对于其他节点必须在使用的节点选项中写入。这个解决方案需要fit
tikz 库:
\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{fit, positioning}
\usepackage{lipsum}
\tikzset{%
N/.style args = {#1/#2}{% N as node (original)
text width=#1/#2, scale=#2,
draw,
outer sep=1mm,
path picture={%
\node[inner sep=1mm, draw,
fit=(path picture bounding box)] {};
}
},
N/.default = 50mm/1,
}% end of tikzset
\begin{document}
\begin{tikzpicture}
\node[N] (one) {hello world (original)};
\node[N=50mm/0.5,
below=2mm of one] {\lipsum[11]};
\end{tikzpicture}
\end{document}
答案2
以下是另一个建议\scalebox
:
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset
{%
ORIGINAL/.style=
{%
draw,
text width=50mm
}
}
\newcommand*\scalenodetext[2][.5]{%
\pgfmathsetmacro\revscale{1/.5}%
\scalebox{#1}{\parbox{\revscale\linewidth}{#2}}%
}
\begin{document}
\begin{tikzpicture}
\node[ORIGINAL](one)
{ % ORIGINAL TEXT-SIZE (MUST BE CUT IN HALF)
hello world (original)
};
\node
[ORIGINAL,below=2mm of one ](two)
{ %
\scalenodetext{hello world (scaled)}
};
\end{tikzpicture}
\end{document}
或者,如果文本缩放时节点的高度不应该改变:
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset
{%
ORIGINAL/.style=
{%
draw,
text width=50mm
}
}
\newsavebox\nodetext
\newcommand*\scalenodetext[2][.5]{%
\savebox\nodetext{\parbox{\linewidth}{#2}}%
\vphantom{\usebox\nodetext}%
\pgfmathsetmacro\revscale{1/.5}%
\scalebox{#1}{\parbox{\revscale\linewidth}{#2}}%
}
\begin{document}
\begin{tikzpicture}
\node[ORIGINAL](one)
{ % ORIGINAL TEXT-SIZE (MUST BE CUT IN HALF)
hello world (original)
};
\node
[ORIGINAL,below=2mm of one ](two)
{ %
\scalenodetext{hello world (scaled)}
};
\end{tikzpicture}
\end{document}