0

The question is a follow-up to this one. Background color for all existing floats

I hope to save the generated yellow background box location information to a new TXT file, as the floating element location annotation information.

As shown in the code below, I have made some preliminary attempts to save location information. After compiling the following code twice, a txt file will be generated in the current file directory. It records the generated coordinate information, but this code generates only the coordinates of the lower left corner, and I need to generate the label of the entire box, which means I also need the information of the height and width of the box. I know that the width of the box is textwidth or columnwidth, how to get the height of the frame?

\documentclass[11pt,a4paper,twocolumn,english]{article}

\usepackage{xcolor}
\usepackage[savepos]{zref}
\newwrite\myoutfile
\immediate\openout\myoutfile=\jobname-myoutfile.txt
\newcounter{mycounter}
\setcounter{mycounter}{1}

\makeatletter
\newif\iffloat@twocolumn

% redefine \@dblfloat
\def\@dblfloat{%
  \if@twocolumn
    \let\reserved@a\@dbflt
    \float@twocolumntrue
  \else
    \let\reserved@a\@float
  \fi
  \reserved@a}

% based on https://tex.stackexchange.com/a/106906
% added \iffloat@twocolumn ... \else ... \fi
\def\foo#1\normalcolor\vbox\bgroup#2!!{%
\def\@xfloat ##1[##2]{#1%
 \normalcolor
      \hbox\bgroup{%
        \color{yellow}%
      \zsavepos{\themycounter}%
        \iffloat@twocolumn
          \leaders\vrule\hskip\textwidth\hskip-\textwidth%  
          \immediate\write\myoutfile{"image",\the\dimexpr\zposx{\themycounter}sp\relax, \the\dimexpr\zposy{\themycounter}sp\relax}%
        \else
          \leaders\vrule\hskip\columnwidth\hskip-\columnwidth%
          \immediate\write\myoutfile{"image",\the\dimexpr\zposx{\themycounter}sp\relax, \the\dimexpr\zposy{\themycounter}sp\relax}%
        \fi}%
      \vbox \bgroup\aftergroup\egroup
 \label{\themycounter}
 \stepcounter{mycounter}%
#2}}
\expandafter\foo\@xfloat{#1}[#2]!!

\makeatother
\title{Two column document with figure underneath title}
\author{abhijit8}

\begin{document}
\maketitle
 \begin{table}[h]
 \small
 \centering
 \caption{Simuleringsresultat av beräkning av $\pi$ genom Monte Carlo.}
 \begin{tabular}{|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}| }
   \hline
   \multicolumn{3}{|c|}{En beräkning av $\pi$ genom Monte Carlo} \\
    \hline
Antal \linebreak partiklar & Värde 1 & Värde 2 \\
 \hline
 $10^0$  & 4    & 4     \\
 $10^1$ & 3,2  & 2,4   \\
 $10^2$ & 3,56 & 2,92    \\
 $10^3$ & 3,208 & 3,160    \\
 $10^4$ & 3,1404  & 3,1288   \\
 $10^5$ & 3,13468  & 3,14724  \\
 $10^6$ & 3,14286  & 3,146348  \\
 $10^7$ & 3,1421208  & 3,141374   \\
 $10^8$ & 3,14138248  & 3,14127896\\
 $10^9$ & 3,1416602762  & 3,1415840040 \\
\hline
\multicolumn{3}{|c|}{$\pi = 3.1415926535 $ }  \\
\hline
\end{tabular}
\end{table}

 \begin{table*}[h]
 \small
 \centering
 \caption{Simuleringsresultat av beräkning av $\pi$ genom Monte Carlo.}
 \begin{tabular}{|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}| }
   \hline
   \multicolumn{6}{|c|}{En beräkning av $\pi$ genom Monte Carlo} \\
    \hline
Antal \linebreak partiklar & Värde 1 & Värde 2 & Värde 3 & Värde 4 &   Värde 5\\
 \hline
 $10^0$  & 4    & 4 &   4  & 4 & 0    \\
 $10^1$ & 3,2  & 2,4   & 4,0  & 3,6  & 3,6  \\
 $10^2$ & 3,56 & 2,92 &  3,32   &  3,04  & 3,36     \\
 $10^3$ & 3,208 & 3,160 &  3,108  & 3,136  & 3,136   \\
 $10^4$ & 3,1404  & 3,1288 & 3,1460  & 3,1572 & 3,1308  \\
 $10^5$ & 3,13468  & 3,14724   & 3,14316 & 3,14124  & 3,14248\\
 $10^6$ & 3,14286  & 3,146348 & 3,142648  &  3,141184  & 3,142144 \\
 $10^7$ & 3,1421208  & 3,141374 & 3,1414892  & 3,1413988  & 3,1416936  \\
 $10^8$ & 3,14138248  & 3,14127896 & 3,14172908  & 3,14154640 & 3,14178604\\
 $10^9$ & 3,1416602762  & 3,1415840040 & 3,1415684520   &  3,1415948080 & 3,1416119840\\
\hline
\multicolumn{6}{|c|}{$\pi = 3.1415926535 $ }  \\
\hline
\end{tabular}
\end{table*}
\end{document}

The following picture is a visual explanation of my needs,In general, I just want to know the height of the background generated by the code \makeatletter ... \makeatother. enter image description here

2
  • From your question list, it seems you want to both (visually) recognize and record positions of latex elements type by type. To avoid an X-Y problem, what's the big picture? Commented Jan 6, 2022 at 9:43
  • Thank you for your question. In fact, the following picture is just a visual explanation of my needs. Actually, I just want to know the height of the background generated by the code of \makeatletter... \makeatother.
    – happy
    Commented Jan 6, 2022 at 9:57

1 Answer 1

1

It seems each time a float is encountered, it's stored in a box \@currbox. That box starts by \@xfloat and ends by \@endfloatbox. Hence the total height of a float is the height and depth of that \@currbox. To record that info, \write code is appended to \@endfloatbox.

\usepackage{xpatch} % for \xapptocmd

\makeatletter
\xapptocmd\@endfloatbox
  {\immediate\write\myoutfile{height: \the\ht\@currbox, depth: \the\dp\@currbox}}
  {}{\PatchFailed}
\makeatother

% optional
\AtEndDocument{%
  \immediate\closeout\myoutfile
}

Full example

\documentclass[11pt,a4paper,twocolumn,english]{article}

\usepackage{xcolor}
\usepackage{xpatch}
\usepackage[savepos]{zref}

\newwrite\myoutfile
\immediate\openout\myoutfile=\jobname-myoutfile.txt
\newcounter{mycounter}
\setcounter{mycounter}{1}


\AtEndDocument{%
  \immediate\closeout\myoutfile
}

\makeatletter
\newif\iffloat@twocolumn

% redefine \@dblfloat
\def\@dblfloat{%
  \if@twocolumn
    \let\reserved@a\@dbflt
    \float@twocolumntrue
  \else
    \let\reserved@a\@float
  \fi
  \reserved@a}

% based on https://tex.stackexchange.com/a/106906
% added \iffloat@twocolumn ... \else ... \fi
\def\foo#1\normalcolor\vbox\bgroup#2!!{%
\def\@xfloat ##1[##2]{#1%
 \normalcolor
      \hbox\bgroup{%
        \color{yellow}%
      \zsavepos{\themycounter}%
        \iffloat@twocolumn
          \leaders\vrule\hskip\textwidth\hskip-\textwidth%  
          \immediate\write\myoutfile{"image",\the\dimexpr\zposx{\themycounter}sp\relax, \the\dimexpr\zposy{\themycounter}sp\relax}%
        \else
          \leaders\vrule\hskip\columnwidth\hskip-\columnwidth%
          \immediate\write\myoutfile{"image",\the\dimexpr\zposx{\themycounter}sp\relax, \the\dimexpr\zposy{\themycounter}sp\relax}%
        \fi}%
      \vbox \bgroup\aftergroup\egroup
 \label{\themycounter}%
 \stepcounter{mycounter}%
#2}}
\expandafter\foo\@xfloat{#1}[#2]!!

\xapptocmd\@endfloatbox
  {\immediate\write\myoutfile{height: \the\ht\@currbox, depth: \the\dp\@currbox}}
  {}{\PatchFailed}
  
\def\end@float{%
  \@endfloatbox
  \ifnum\@floatpenalty <\z@
    \@largefloatcheck
    \@cons\@currlist\@currbox
    \ifnum\@floatpenalty <-\@Mii
      \penalty -\@Miv
      \@tempdima\prevdepth
      \vbox{}%
      \prevdepth\@tempdima
      \penalty\@floatpenalty
    \else
      \vadjust{\penalty -\@Miv \vbox{}\penalty\@floatpenalty}\@Esphack
    \fi
  \fi
}
\makeatother

\title{Two column document with figure underneath title}
\author{abhijit8}

\begin{document}
\maketitle
 \begin{table}[h]
 \small
 \centering
 \caption{Simuleringsresultat av beräkning av $\pi$ genom Monte Carlo.}
 \begin{tabular}{|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}| }
   \hline
   \multicolumn{3}{|c|}{En beräkning av $\pi$ genom Monte Carlo} \\
    \hline
Antal \linebreak partiklar & Värde 1 & Värde 2 \\
 \hline
 $10^0$  & 4    & 4     \\
 $10^1$ & 3,2  & 2,4   \\
 $10^2$ & 3,56 & 2,92    \\
 $10^3$ & 3,208 & 3,160    \\
 $10^4$ & 3,1404  & 3,1288   \\
 $10^5$ & 3,13468  & 3,14724  \\
 $10^6$ & 3,14286  & 3,146348  \\
 $10^7$ & 3,1421208  & 3,141374   \\
 $10^8$ & 3,14138248  & 3,14127896\\
 $10^9$ & 3,1416602762  & 3,1415840040 \\
\hline
\multicolumn{3}{|c|}{$\pi = 3.1415926535 $ }  \\
\hline
\end{tabular}
\end{table}

 \begin{table*}[h]
 \small
 \centering
 \caption{Simuleringsresultat av beräkning av $\pi$ genom Monte Carlo.}
 \begin{tabular}{|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}|p{2.1cm}| }
   \hline
   \multicolumn{6}{|c|}{En beräkning av $\pi$ genom Monte Carlo} \\
    \hline
Antal \linebreak partiklar & Värde 1 & Värde 2 & Värde 3 & Värde 4 &   Värde 5\\
 \hline
 $10^0$  & 4    & 4 &   4  & 4 & 0    \\
 $10^1$ & 3,2  & 2,4   & 4,0  & 3,6  & 3,6  \\
 $10^2$ & 3,56 & 2,92 &  3,32   &  3,04  & 3,36     \\
 $10^3$ & 3,208 & 3,160 &  3,108  & 3,136  & 3,136   \\
 $10^4$ & 3,1404  & 3,1288 & 3,1460  & 3,1572 & 3,1308  \\
 $10^5$ & 3,13468  & 3,14724   & 3,14316 & 3,14124  & 3,14248\\
 $10^6$ & 3,14286  & 3,146348 & 3,142648  &  3,141184  & 3,142144 \\
 $10^7$ & 3,1421208  & 3,141374 & 3,1414892  & 3,1413988  & 3,1416936  \\
 $10^8$ & 3,14138248  & 3,14127896 & 3,14172908  & 3,14154640 & 3,14178604\\
 $10^9$ & 3,1416602762  & 3,1415840040 & 3,1415684520   &  3,1415948080 & 3,1416119840\\
\hline
\multicolumn{6}{|c|}{$\pi = 3.1415926535 $ }  \\
\hline
\end{tabular}
\end{table*}

\end{document}

This will write

"image",72.26999pt, 383.81009pt
height: 204.3333pt, depth: 0.0pt
"image",72.26999pt, 324.51009pt
height: 190.73329pt, depth: 0.0pt

to \jobname-myoutfile.txt.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .