1

The following code by @cis does something wonderfully, it measures the distance between two points on a picture, and then creates a ruler around the picture.

\documentclass[a4paper, landscape]{article}
\usepackage[margin=2cm, showframe=false]{geometry} 
\usepackage{mwe} % Dummy images

\usepackage{tikz} \usetikzlibrary{calc} \pgfkeys{/tikz/savevalue/.code 2 args={\global\edef#1{#2}}}

\def\ShowHelps{1} % 1 'yes' 0 'no' \begin{document} \centering\begin{tikzpicture}[font=\sffamily, Help/.style={font=\Huge, red} ] \node[anchor=south west,inner sep=0pt] (myimage) at (0,0) {
\IfFileExists{cmHgI.jpg} {\includegraphics[scale=0.15]{cmHgI.jpg}} {\includegraphics[width=0.75\textwidth]{example-image.jpg}} }; \begin{scope}[x={(myimage.south east)},y={(myimage.north west)}] % Restrict the draw area \clip (0,0) rectangle (1,1); % Frame around image - optional \draw[] (0,0) rectangle (1,1); % optional % CoSy inside node \newcommand\ShowHelpCoSy{ \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1); \foreach \x in {0,1,...,9} { \node[anchor=north] at (\x/10,0) {0.\x}; } \foreach \y in {0,1,...,9} { \node[anchor=east] at (0,\y/10) {0.\y}; } } \ifnum\ShowHelps=1 \ShowHelpCoSy \fi% \ifnum\ShowHelps=1\fill[Help] (0.5,0.75) circle(3pt) node[above]{(0.5,0.75)};\fi% <-- Position Test

% Now the stuff: \path[] (0.705,0.4) coordinate(U1) -- (0.705,0.519) coordinate(U2); \ifnum\ShowHelps=1 \draw[Help] (U1) circle(2pt) node[label=left:U1]{} -- (U2) circle(2pt) node[label=left:U2]{};\fi % show unitlength line

\path let \p1=($(U2)-(U1)$) in \pgfextra{ \pgfmathsetlengthmacro{\unitlength}{veclen(\x1,\y1)} } [savevalue={\u}{\unitlength}]; \ifnum\ShowHelps=1 \node[Help] at (0.725,0.25) {unitlength U1U2 = \u}; \fi%<--- show unitlength \ifnum\ShowHelps=1 \draw[blue, transform canvas={xshift=2mm}] (U1) -- +(0,\u);\fi % Test

% Small Ruler \draw[very thick] (0.3,0.35) coordinate(A) -- +(0,3\u); \foreach \n in {0,1,...,3}{%% \draw[very thick] ([yshift=\n\u]A) -- +(-3mm,0) node[left]{\n} \ifnum\n=0 node[anchor=west, right=3mm]{cm}\fi; }%% \foreach \n in {0.1,0.2,...,3}{%% \draw[] ([yshift=\n*\u]A) -- +(-1.5mm,0); }%%

% Image Ruler y \draw[very thick] (0,0) coordinate(B) -- +(0,10\u); \foreach \n in {0,1,...,10}{%% \draw[very thick] ([yshift=\n\u]B) -- +(3mm,0) node[right]{ \ifnum\n=0 \else \ifnum\n=1 \n,cm \else \n \fi\fi}; }%% \foreach \n in {0.1,0.2,...,10}{%% \draw[] ([yshift=\n*\u]B) -- +(1.5mm,0); }%%

% Image Ruler x \draw[very thick] (0,1) coordinate(C) -- (15\u,1); \foreach \n in {0,1,...,15}{%% \pgfmathsetmacro\Color{\n<9 ? "black" : "white"} \draw[very thick, \Color] ([xshift=\n\u]C) -- +(0,-3mm) node[below, text=\Color, inner sep=1pt]{ \ifnum\n=0 \else \ifnum\n=1 \n,cm \else \n \fi\fi}; }%% \foreach \n in {0.1,0.2,...,15}{%% \pgfmathsetmacro\Color{\n<9 ? "black" : "white"} \draw[\Color] ([xshift=\n*\u]C) -- +(0,-1.5mm); }%% \end{scope} \end{tikzpicture} \end{document}

My question is: how can we turn this into a macro, or simplify this snippet, such that it can be used over and over and over.

Namely, such that there is:

  • 1 picture per page (nothing else on the page, such that the page is 'cropped' tightly around the picture);
  • the distance is measured only on the first picture (only 1 time, the user is going to determine U1 and U2, and the distance between them);
  • the size of the virtual rulers should thus be saved, and should be shown on every subsequent picture (this is good, because all of the pictures are sized evenly).
O0123
  • 1,773

1 Answers1

1

I am not sure, whether I understand, but to apply a code to several images you can use a foreach-loop:

\foreach \image/\rulerpos in {
{example-image-a}/{0.3,0.25},% 
{example-image-b}/{0.8,0.25},%
{example-image-c}/{0.3,0.4},%
{AnotherPicture}/{0.1,0.1}%
}{
..........
..........
..........
}

First answer:

enter image description here

\documentclass[a4paper, landscape]{article}
\usepackage[margin=2cm, showframe=false]{geometry} 
\usepackage{mwe} % Dummy images

\usepackage{tikz} \usetikzlibrary{calc} \pgfkeys{/tikz/savevalue/.code 2 args={\global\edef#1{#2}}}

\def\ShowHelps{0} % 1 'yes' 0 'no' \begin{document} \foreach \image/\rulerpos in { {example-image-a}/{0.3,0.25},% {example-image-b}/{0.8,0.25},% {example-image-c}/{0.3,0.4},% {AnotherPicture}/{0.1,0.1}% }{%%%%%%%%%%%%%%% \begin{center} \begin{tikzpicture}[font=\sffamily, Help/.style={font=\Huge, red} ] \node[anchor=south west,inner sep=0pt] (myimage) at (0,0) {
\IfFileExists{\image.jpg} {\includegraphics[width=0.75\textwidth]{\image.jpg}} {\includegraphics[width=0.75\textwidth]{example-image.jpg}} }; \begin{scope}[x={(myimage.south east)},y={(myimage.north west)}] % Restrict the draw area \clip (0,0) rectangle (1,1); % Frame around image - optional \draw[] (0,0) rectangle (1,1); % optional % CoSy inside node \newcommand\ShowHelpCoSy{ \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1); \foreach \x in {0,1,...,9} { \node[anchor=north] at (\x/10,0) {0.\x}; } \foreach \y in {0,1,...,9} { \node[anchor=east] at (0,\y/10) {0.\y}; } } \ifnum\ShowHelps=1 \ShowHelpCoSy \fi% \ifnum\ShowHelps=1\fill[Help] (0.5,0.75) circle(3pt) node[above]{(0.5,0.75)};\fi% <-- Position Test

% Now the stuff: \path[] (0.705,0.4) coordinate(U1) -- (0.705,0.519) coordinate(U2); \ifnum\ShowHelps=1 \draw[Help] (U1) circle(2pt) node[label=left:U1]{} -- (U2) circle(2pt) node[label=left:U2]{};\fi % show unitlength line

\path let \p1=($(U2)-(U1)$) in \pgfextra{ \pgfmathsetlengthmacro{\unitlength}{veclen(\x1,\y1)} } [savevalue={\u}{\unitlength}]; \ifnum\ShowHelps=1 \node[Help] at (0.725,0.25) {unitlength U1U2 = \u}; \fi%<--- show unitlength \ifnum\ShowHelps=1 \draw[blue, transform canvas={xshift=2mm}] (U1) -- +(0,\u);\fi % Test

% Small Ruler \draw[very thick] (\rulerpos) coordinate(A) -- +(0,3\u); \foreach \n in {0,1,...,3}{%% \draw[very thick] ([yshift=\n\u]A) -- +(-3mm,0) node[left]{\n} \ifnum\n=0 node[anchor=west, right=3mm]{cm}\fi; }%% \foreach \n in {0.1,0.2,...,3}{%% \draw[] ([yshift=\n*\u]A) -- +(-1.5mm,0); }%%

% Image Ruler x \draw[very thick] (0,0) coordinate(B) -- +(0,10\u); \foreach \n in {0,1,...,10}{%% \draw[very thick] ([yshift=\n\u]B) -- +(3mm,0) node[right]{ \ifnum\n=0 \else \ifnum\n=1 \n,cm \else \n \fi\fi}; }%% \foreach \n in {0.1,0.2,...,10}{%% \draw[] ([yshift=\n*\u]B) -- +(1.5mm,0); }%%

% Image Ruler y \draw[very thick] (0,1) coordinate(C) -- (15\u,1); \foreach \n in {0,1,...,15}{%% \pgfmathsetmacro\Color{\n==\n ? "black" : "white"} \draw[very thick, \Color] ([xshift=\n\u]C) -- +(0,-3mm) node[below, text=\Color, inner sep=1pt]{ \ifnum\n=0 \else \ifnum\n=1 \n,cm \else \n \fi\fi}; }%% \foreach \n in {0.1,0.2,...,15}{%% \pgfmathsetmacro\Color{\n==\n ? "black" : "white"} \draw[\Color] ([xshift=\n*\u]C) -- +(0,-1.5mm); }%% \end{scope} \end{tikzpicture} \end{center} \newpage }%%%%%%%%%%%%%%%%%%%%%%%% \end{document}

Second Answer: In this version the images move to the page center.

enter image description here

\documentclass[a4paper, landscape]{article}
\usepackage[margin=2cm, showframe=false]{geometry} 
\usepackage{mwe} % Dummy images

\usepackage{tikz} \usepackage{tikzpagenodes} % current page text area.center \usetikzlibrary{calc} \pgfkeys{/tikz/savevalue/.code 2 args={\global\edef#1{#2}}}

\def\ShowHelps{1} % 1 'yes' 0 'no' \begin{document} \foreach \image/\rulerpos/\captiontext in { {example-image-a}/{0.3,0.25},% %{example-image-b}/{0.8,0.25},% %{example-image-c}/{0.3,0.4},% %{cmHgI}/{0.1,0.1},% {AnotherPicture}/{0.2,0.1}% }{%%%%%%%%%%%%%%% %\centering \begin{tikzpicture}[ remember picture, overlay, % page center 1/3 shift={(current page.center)}, % page center 2/3 font=\sffamily, Help/.style={font=\Huge, red} ] \node[anchor=center, %south west, inner sep=0pt] (myimage) at (0,0) {
\IfFileExists{\image.jpg} {\includegraphics[width=0.75\textwidth]{\image.jpg}} {\includegraphics[width=0.75\textwidth]{example-image.jpg}} }; \begin{scope}[x={($2(myimage.east)$)},y={($2(myimage.north)$)}, shift={(myimage.south west)} % page center 3/3 ] % Restrict the draw area \clip (0,0) rectangle (1,1); % Frame around image - optional \draw[] (0,0) rectangle (1,1); % optional % CoSy inside node \newcommand\ShowHelpCoSy{ \draw[help lines,xstep=.1,ystep=.1] (0,0) grid (1,1); \foreach \x in {0,1,...,9} { \node[anchor=north] at (\x/10,0) {0.\x}; } \foreach \y in {0,1,...,9} { \node[anchor=east] at (0,\y/10) {0.\y}; } } \ifnum\ShowHelps=1 \ShowHelpCoSy \fi% \ifnum\ShowHelps=1\fill[Help] (0.5,0.75) circle(3pt) node[above]{(0.5,0.75)};\fi% <-- Position Test

% Now the stuff: \path[] (0.705,0.4) coordinate(U1) -- (0.705,0.519) coordinate(U2); \ifnum\ShowHelps=1 \draw[Help] (U1) circle(2pt) node[label=left:U1]{} -- (U2) circle(2pt) node[label=left:U2]{};\fi % show unitlength line

\path let \p1=($(U2)-(U1)$) in \pgfextra{ \pgfmathsetlengthmacro{\unitlength}{veclen(\x1,\y1)} } [savevalue={\u}{\unitlength}]; \ifnum\ShowHelps=1 \node[Help] at (0.725,0.25) {unitlength U1U2 = \u}; \fi%<--- show unitlength \ifnum\ShowHelps=1 \draw[blue, transform canvas={xshift=2mm}] (U1) -- +(0,\u);\fi % Test

% Small Ruler \draw[very thick] (\rulerpos) coordinate(A) -- +(0,3\u); \foreach \n in {0,1,...,3}{%% \draw[very thick] ([yshift=\n\u]A) -- +(-3mm,0) node[left]{\n} \ifnum\n=0 node[anchor=west, right=3mm]{cm}\fi; }%% \foreach \n in {0.1,0.2,...,3}{%% \draw[] ([yshift=\n*\u]A) -- +(-1.5mm,0); }%%

% Image Ruler x \draw[very thick] (0,0) coordinate(B) -- +(0,10\u); \foreach \n in {0,1,...,10}{%% \draw[very thick] ([yshift=\n\u]B) -- +(3mm,0) node[right]{ \ifnum\n=0 \else \ifnum\n=1 \n,cm \else \n \fi\fi}; }%% \foreach \n in {0.1,0.2,...,10}{%% \draw[] ([yshift=\n*\u]B) -- +(1.5mm,0); }%%

% Image Ruler y \draw[very thick] (0,1) coordinate(C) -- (15\u,1); \foreach \n in {0,1,...,15}{%% \pgfmathsetmacro\Color{\n==\n ? "black" : "white"} \draw[very thick, \Color] ([xshift=\n\u]C) -- +(0,-3mm) node[below, text=\Color, inner sep=1pt]{ \ifnum\n=0 \else \ifnum\n=1 \n,cm \else \n \fi\fi}; }%% \foreach \n in {0.1,0.2,...,15}{%% \pgfmathsetmacro\Color{\n==\n ? "black" : "white"} \draw[\Color] ([xshift=\n\u]C) -- +(0,-1.5mm); }%% \end{scope} \end{tikzpicture} %\vspace{\fill} \newpage }%%%%%%%%%%%%%%%%%%%%%%%% \end{document}

cis
  • 8,073
  • 1
  • 16
  • 45
  • Yes, brilliant! Now, remains only the cropping challenge. – O0123 Aug 29 '20 at 16:05
  • Yes, I have no idea that "such that the page is 'cropped' tightly around the picture" depends in my opinion from the pictures. For pictures that are too large, graphix, which provides includegraphics, has a cutting function (I think 'trim'). – cis Aug 29 '20 at 16:12
  • I think you will use \includegraphics[scale=<same scale factor here>]{} so the pictures should not be quite different. – cis Aug 29 '20 at 16:14
  • If I choose \includegraphics[scale=1]{} now, then before each image is inserted an extra unwanted blank page. How to prevent this please? – O0123 Aug 29 '20 at 16:50
  • Due to my tests he makes a newpage, if the pictures are too big, so you could need scale=0.xxx – cis Aug 29 '20 at 17:02
  • For your example images, width=0.942\textwidth seems to work, when I remove the \newpage and \usepackage[margin=0cm]{geometry}, but there are still margins on the left and right of the image. --- But I assume if we adjust the page size according to the image size, it will be fine So no worries. :) – O0123 Aug 29 '20 at 17:06
  • If you already have the pdf file ready with everything you want and you only want to crop the blank margins, maybe you can use pdfcrop yourfile.pdf from a terminal – Pablo González L Aug 30 '20 at 23:25