
我試圖弄清楚如何最好地向我的圖像添加比例尺,例如在這些美妙的雪花圖像中(全尺寸圖像有點大,但這是我想要做的最好的例子):
一種可能性是使用不同的程序插入比例尺。但是,我想確保文字美觀且清晰,而不是將其保存為光柵圖像的一部分。另外,如果我事先插入比例尺,那麼當 LaTeX 縮放圖像時文字將被調整大小。
到目前為止,我還沒有找到任何現有的命令或套件可以輕鬆做到這一點。我希望能夠指定影像的真實世界大小,並計算比例尺的大小。
如果我必須編寫自己的宏,我想我會從過圖包
我的方向正確嗎?
編輯2:
熟悉 TikZ 後,我想出了以下宏:
\usepackage{tikz}
\usepackage{siunitx}
% Inserts a scale bar into an image
% Optional argument 1: the colour of the bar and text
% Argument 2: an \includegraphics command
% Argument 3: the real world width of the image
% Argument 4: the length of the scale bar
% Argument 5: the units in which the scale bar is measured
\newcommand{\scalebar}[5][white]{
\begin{tikzpicture}
\draw (0,0) node[anchor=south west,inner sep=0] (image) { #2 };
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\fill [#1] (0.05,0.2cm) rectangle (#4/#3+0.05,0.4cm);
\draw [#1] (0.05,0.4cm) node[anchor=south west] { \SI{#4}{#5} };
\end{scope}
\end{tikzpicture}
}
我在這裡使用單位組合(厘米和圖像尺寸的分數)。我覺得這方面可以更優雅一點,但我現在所擁有的已經足以滿足我的目的了。
答案1
我對你的巨集進行了一些調整,以在文字變得難以閱讀時添加透明背景:
\newcommand{\scalebarbackground}[6][white]{
\begin{tikzpicture}
\node[anchor=south west,inner sep=0] (image) { #2 };
\begin{scope}[x={(image.south east)},y={(image.north west)}]
\fill [fill=blue, fill opacity=0.5] (0.04,1.3em) rectangle (#5*#4/#3+0.04,0.1em);
\draw [#1, line width=0.2em] (0.04,1.2em) -- node[below,inner sep=0.1em, font=\footnotesize] {\SI{#5}{#6}} (#5*#4/#3+0.04,1.2em);
\end{scope}
\end{tikzpicture}
}
生成的比例尺如下圖所示:
有關更多資訊和沒有背景的比例尺,請參閱我的要點:https://gist.github.com/rbnvrw/00312251b756f6b48084#file-latexscalebars-md