I'm using \def\x{1068}
to have x
as a variable in my script to generate scalebars onto images with TikZ. That works like a charm for me.
A working example is pasted below, even though the image does not look very nice :)
\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{tikz}
\usepackage{siunitx}
\usepackage[graphics,tightpage,active]{preview}
\PreviewEnvironment{tikzpicture}
\newcommand{\imsize}{\linewidth}
\newlength\imagewidth % needed for scalebars
\newlength\imagescale % ditto
\begin{document}%
%-------------
\pgfmathsetlength{\imagewidth}{\imsize}%
\pgfmathsetlength{\imagescale}{\imagewidth/1728}%
\def\x{1068}% scalebar-x at golden ratio of x=1728px
\def\y{320}% scalebar-y at 90% of height of y=356px
\def\shadow{11}% shadow parameter for scalebar
\begin{tikzpicture}[x=\imagescale,y=-\imagescale]
\clip (0,0) rectangle (1728,356);
\node[anchor=north west, inner sep=0pt, outer sep=0pt] at (0,0) {\includegraphics[width=\imagewidth]{image}};
% 279px = 1.7819mm > 100px = 638um > 78px = 500um, 16px = 100um
\draw[|-|,blue,thick] (791,151) -- (1020,311) node [sloped,midway,above,fill=white,semitransparent,text opacity=1] {\SI{1.7819}{\milli\meter} (1204px) TEMPORARY!};
\draw[|-|,thick] (\x+\shadow,\y+\shadow) -- (\x+78+\shadow,\y+\shadow) node [midway, above] {\SI{500}{\micro\meter}};
\draw[|-|,white,thick] (\x,\y) -- (\x+78,\y) node [midway,above] {\SI{500}{\micro\meter}};
\draw[color=red, anchor=south west] (0,356) node [fill=white, semitransparent] {Legend} node {Legend};
\end{tikzpicture}%
%-------------
\end{document}%
pgfplots
tag, do you have anything particular in mind that concerns drawing things inpgfplots
. There are some special things inside itsaxis
environments, like theaxis cs
andrel axis cs
coordinate systems that might be helpful for you, so maybe you could edit your question to give more details (or remove thepgfplots
tag if you actually meant TikZ in general).x1
,x2
,y1
, andy2
. If you just want to vary them explicitly, then the\newcommand
approach is as good as any. If they are coordinates, then the\coordinate
approach is best. But if they are to be computed from something else, then something a little more complicated (but not much) is needed; however, exactly what depends on how you want to specify them.tkz-euclide
that he refers to in an answer to another question could be helpful for you? There are quite a few examples on his website.