4

I need to insert text in the precise place on a page (when I print it and measure with a ruler, I would like it to be exactly as written in the editor). I try with textpos but it doesn't work on the way I wanted.

Let me draw one example: Xyz

I want to specify which red point I gave as a parameter x,y and than write some text. For example I choice left up red point, write (0mm,0mm){Xyz} and string "Xyz" will be exactly in left up corner (without margin) of paper, and so on.

I am a beginner in TeX, so please help as simple as possible.

Zarko
  • 296,517
chose
  • 97

3 Answers3

5

Based on my answer at What are the ways to position things absolutely on the page?.

EDITED to handle x alignment with different font sizes.

This approach can be combined in a macro with alignments in the form of

\aligntext[<font-size>]{<x>}{<y>}{<l/c/r>}{<t/b/B/x>}{<content>}

where <l/c/r> is for left/center/right alignment and <t/b/B/x> is for top/bottom/BASELINE/ex alignment, relative to the specified (x,y) coordinate. By "ex" alignment, I mean alignment to a vertical location 1ex above the baseline. These four vertical alignments correspond to the red dot locations of the OP's figure.

To show it best, I also provide an \aligntextref macro with the same argument stream that places a red dot centered at the given (x,y) location, so that the alignment can be checked.

The EDIT was necessary because when changing fontsize in the content of the argument, the size of the 1ex shift (for x alignment) did not revise itself to the new font size until too late. As shown in the updated MWE, the <fontsize> in the optional argument can be given as \Huge, \tiny, etc. However, it can also be given as \fontsize{16pt}{16pt}\selectfont, for example.

Here is the MWE.

\documentclass{article}
\usepackage{everypage,xcolor}
\usepackage{stackengine}
% THESE ARE LaTeX DEFAULTS; CAN CHANGE IF NEEDED.
\def\PageTopMargin{1in}
\def\PageLeftMargin{1in}
\newcommand\atxy[3]{%
 \AddThispageHook{\smash{\hspace*{\dimexpr-\PageLeftMargin-\hoffset+#1\relax}%
  \raisebox{\dimexpr\PageTopMargin+\voffset-#2\relax}{#3}}}}
\newcommand\aligntext[6][\normalsize]{%
  \if B#5\atxy{#2}{#3}{#1\makebox[0pt][#4]{#6}}\else%
  \if x#5\atxy{#2}{#3}{#1\raisebox{-1ex}{\makebox[0pt][#4]{#6}}}\else%
  \if b#5\atxy{#2}{#3}{#1\abovebaseline[0pt]{\makebox[0pt][#4]{#6}}}\else%
  \if t#5\atxy{#2}{#3}{#1\belowbaseline[0pt]{\makebox[0pt][#4]{#6}}}\fi\fi\fi\fi%
}
\newcommand\aligntextref[6][\normalsize]{%
  \aligntext[#1]{#2}{#3}{#4}{#5}{#6}%
  \aligntext[#1]{#2}{#3}{c}{B}{\color{red}\makebox[0pt]{\rule[-.4pt]{.8pt}{.8pt}}}%
}
\begin{document}
\aligntext{0\paperwidth}{0\paperheight}{l}{t}{Top left alignment}
\aligntext{.5\paperwidth}{0\paperheight}{c}{x}{Top x alignment}
\aligntext{1\paperwidth}{0\paperheight}{r}{t}{Top right alignment}
\aligntext{0\paperwidth}{1\paperheight}{l}{b}{Bottom left alignment}
\aligntext{.5\paperwidth}{1\paperheight}{c}{B}{BASELINE center alignment}
\aligntext{1\paperwidth}{1\paperheight}{r}{b}{Bottom right alignment}
\mbox{}
\aligntextref{100pt}{100pt}{r}{t}{X}
\aligntextref{120pt}{100pt}{l}{t}{fg}
\aligntextref{110pt}{100pt}{c}{t}{x}
\aligntextref{100pt}{115pt}{r}{x}{X}
\aligntextref{120pt}{115pt}{l}{x}{fg}
\aligntextref{110pt}{115pt}{c}{x}{x}
\aligntextref{100pt}{130pt}{r}{B}{X}
\aligntextref{120pt}{130pt}{l}{B}{fg}
\aligntextref{110pt}{130pt}{c}{B}{x}
\aligntextref{100pt}{145pt}{r}{b}{X}
\aligntextref{120pt}{145pt}{l}{b}{fg}
\aligntextref{110pt}{145pt}{c}{b}{x}

\aligntextref[\Huge]{110pt}{185pt}{l}{x}{x alignment Huge}
\aligntextref[\tiny]{110pt}{185pt}{r}{x}{x alignment tiny}

\aligntext{140pt}{100pt}{l}{t}{Top alignment}
\aligntext{140pt}{115pt}{l}{x}{Ex alignment}
\aligntext{140pt}{130pt}{l}{B}{Baseline alignment}
\aligntext{140pt}{145pt}{l}{b}{bottom alignment}
\end{document}

enter image description here

The EDIT provides the fix for the next image.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Note: A page composed completely of \aligntext calls will produce no output unless something is added as normal text to the page. That is why the MWE has a \mbox{} mixed in, so as to trigger the normal page production.

  • Thank you, it is great. I have one problem, when i change font size (e.g. \Huge{Xxfg} ) didn't work, it's only work fine with normal font size. (I checked \aligntext{.5\paperwidth}{0\paperheight}{c}{x}{\Huge{Top x alignment}}). – chose Jul 08 '16 at 15:36
  • 1
    @chose I presume the problem is with the x alignment? When you change fontsize with, for example, \Huge, the definition of 1ex is still in the base fontsize. – Steven B. Segletes Jul 08 '16 at 15:55
  • 1
    @chose Please see edit. – Steven B. Segletes Jul 08 '16 at 16:15
2

I've never used texpos before, but the example in manual's last page shows that

\documentclass{article}

\usepackage[absolute]{textpos}

\setlength{\TPHorizModule}{30mm}
\setlength{\TPVertModule}{\TPHorizModule}
\textblockorigin{0mm}{0mm} % start everything near the top-left corner
\setlength{\parindent}{0pt}

\begin{document}
\begin{textblock}{1}(0,0)
Xyz
\end{textblock}
\end{document}

will place Xyz on page's top left corner.

enter image description here

Ignasi
  • 136,588
1

I think you could use the node anchors to fix its position, but maybe with some more code you could use the "circles" to position it.

Output

enter image description here

Code

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{calc}

\usetikzlibrary{calc, intersections}

\tikzset{
    linex/.style={green!50!black, shorten <=-3mm, shorten >=-3mm}
}

\newlength\txtht
\setlength\txtht{\heightof{a}}

\newcommand\addtext[3][]{%
    \tikz[overlay, remember picture, shift={(current page.south west)}, line width=.2pt]{%
        \node[inner sep=0, outer sep=0, #1] (text) at (#2) {#3};
    \draw[linex, name path global=ray1] (text.south west) -- (text.south east);
    \draw[linex, name path global=ray2] (text.south west) -- (text.north west);
    \draw[linex, name path global=ray3] (text.north west) -- (text.north east);
    \draw[linex, name path global=ray4] (text.south east) -- (text.north east);
    \draw[linex, name path global=ray5] (text.south west) -- (text.north west);
    \draw[linex, name path global=ray6] (text.south) -- (text.north);
    \draw[linex, name path global=ray7] (text.base west) -- (text.base east);
    \draw[linex, name path global=ray8] ($(text.base west)+(0,\txtht)$) -- ($(text.base east)+(0,\txtht)$);
    \foreach \x in {1,...,8}{%
        \foreach \y in {2,...,8}{%
            \fill[red, name intersections={of={ray\x} and {ray\y}}] (intersection-1) circle (.4pt);
        }}
    }
}

\pagestyle{empty}

\begin{document}
\addtext{4,5}{Xyz}
\end{document}
Alenanno
  • 37,338