保存所有现有浮动的位置信息

保存所有现有浮动的位置信息

该问题是此问题的后续。 所有现有浮动的背景颜色

希望将生成的黄色背景框位置信息保存到一个新的TXT文件中,作为浮动元素位置标注信息。

如下代码所示,我对保存位置信息做了一些初步的尝试,下面的代码编译两次之后会在当前文件目录下生成一个txt文件,里面记录了生成的坐标信息,但是这个代码只生成了左下角的坐标,而我需要生成整个框的标签,也就是说我还需要框的高宽信息,我知道框的宽度是textwidth或者columnwidth,那么如何获取框的高度呢?

\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}

下图直观的说明了我的需求,总体来说我只是想知道代码生成的背景的高度\makeatletter ... \makeatother在此处输入图片描述

答案1

似乎每次遇到浮点数时,它都会被存储在一个框中\@currbox。该框以 开始\@xfloat,以 结束\@endfloatbox。因此,浮点数的总高度就是 的高度和深度\@currbox。为了记录该信息,\write将代码附加到\@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
}

完整示例

\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}

这将写

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

\jobname-myoutfile.txt

相关内容