2

I want to insert text in a beamer slide using an invisible tikz matrix of fixed width (.85\paperwidth), 2 columns (~ 3cm) on the upper left part and similarly on the upper right part. Only one matrix, with a middle empty column used as spacer. This way the two blocks will be symmetrically placed.

The main problem is the calculation of the width of the middle column. I thought of setting minimum width = .8\paperwidth for the matrix settings and setting the width of col. 1,2,4,5 individually would automatically adjust the width of the middle column so that the whole matrix width is equal to my setting, but that does not work. Also I've not been able to setup the use of width("x") for setting the width of columns 1,2,4,5.

\documentclass{beamer}
\newlength{\slw}\setlength{\slw}{160mm}
\newlength{\slh}\setlength{\slh}{100mm}
\geometry{verbose,papersize={\slw,\slh}}

\usepackage{tikz,calc,xfp} \usetikzlibrary{calc,matrix} \tikzset{ allmatrix/.style = {matrix of nodes, rounded corners = 1mm, nodes in empty cells,matrix anchor=#1, row sep=-\pgflinewidth, column sep=-\pgflinewidth, ampersand replacement=&, nodes={outer sep=0pt}, text height=1.5ex, text depth=.25ex}, table/.style args= {#1/#2}{ column #1/.append style={nodes={align=left, minimum width={\fpeval{#2 + 2 * 2mm}pt}, text width=#2}}, }}

\begin{document} \begin{frame} \begin{tikzpicture}[overlay,remember picture]

\matrix at (current page.north) [shift={(0,-2)}, allmatrix=north, nodes={draw}, table/.list={1/3cm, 2/3cm, 4/3cm, 5/3cm}] (mytab) % table/.list={1/width("le monde"), % 2/width("new york times"), % 4/width("le figaro"), % 5/width("southern echo")}] (near) { le monde & new york times & & le figaro & southern echo\ innadu & le maine & & penpavar & the hindu \ times & spectrum & & science & nature \ }; \end{tikzpicture} \end{frame} \end{document}

  • Don't use \fpeval. Just use #2+2*2mm directly. PGF/TikZ evaluates that on its own. Instead of 2mm use \pgfkeysvalueof{/pgf/inner xsep}. – Qrrbrbirlbel Nov 16 '22 at 08:32
  • Instead of an empty middle column (which needs to have something in it otherwise it has no width) you can just set the column sep between column 2 and 3 to the remainder of the width: .85\paperwidth minus all the text widths minus all the inner xseps. – Qrrbrbirlbel Nov 16 '22 at 08:36
  • since i use a list to pass the width of the columns, how would you express that calculation? – user1850133 Nov 16 '22 at 13:12

3 Answers3

4

Don't use \fpeval unless you really need it (and it doesn't understand width("…") which is a PGFmath function). PGF/TikZ throws almost everything in PGFmath anyway so there's no need for another package to evaluate things (unless you need high precision).

In the code below, I've removed the setting of minimum width completely since text width already specifies the width of the nodes. PGF just adds the value of /pgf/inner xsep on both sides.

Instead of a pseudo-empty column I'm choosing to set the column sep between the second and the third column (out of four). For this, I preset the value-key /tikz/user1850133 column sep with .85\textwidth and inside the table+/.list execution the width("…") as well as twice the inner xseps will be subtracted. (To be precise I'm just building a value of

.85\textwidth-width("le monde")-2*\pgfkeysvalueof{/pgf/inner xsep}-
             ⋮
             -width("southern echo")-2*\pgfkeysvalueof{/pgf/inner xsep}

The actual evaluation is done by PGFmath somewhere deep in the building of the matrix.

(The calculation possibly needs () around \pgfkeysvalueof{…} if that contains more complex values than the default .3333em.)


I've added

every outer matrix/.append style={inner sep=+0pt, outer sep=+0pt}

so that the actual matrix is as tight as possible since the matrix itself has its own inner (and outer) seps.

I've also changed the down-shift to be 2cm in the canvas coordinate system (instead of the xy coordinate system) because you only deal with actual lengths.


The second frame is built by using two seperate matrices whose outer border is set .85\textwidth from each other. This would make it easier to create matrices that have different numbers of columns without having to figure out which one is the column before the split.

The output is slightly different, though I chalk this up to imprecisions of calculations of lengths and distances.

Code

\documentclass{beamer}
\geometry{verbose,papersize={160mm,100mm}}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
  allmatrix/.style = {matrix of nodes, rounded corners = 1mm,
                      nodes in empty cells, matrix anchor=#1,
                      every outer matrix/.append style={inner sep=+0pt, outer sep=+0pt},
                      row sep=-\pgflinewidth, column sep=-\pgflinewidth,
                      ampersand replacement=\&, nodes={outer sep=0pt},
                      text height=1.5ex, text depth=.25ex},
  table/.style args= {#1/#2}{
    column #1/.append style={nodes={align=left, text width=#2}}},
  table+/.style args= {#1/#2}{
    column #1/.append style={nodes={align=left, text width=#2}},
    user1850133 column sep/.append={-#2-2*\pgfkeysvalueof{/pgf/inner xsep}}}}

\begin{document} \begin{frame} \begin{tikzpicture}[overlay,remember picture] \matrix at (current page.north) [shift={(0,-2cm)}, allmatrix=north, nodes={draw}, column 2/.append style={column sep=\pgfkeysvalueof{/tikz/user1850133 column sep}}, user1850133 column sep/.initial=.85\textwidth, table+/.list={1/width("le monde"), 2/width("new york times"), 3/width("penpavar"), 4/width("southern echo")}] (near) { le monde & new york times & le figaro & southern echo\ innadu & le maine & penpavar & the hindu \ times & spectrum & science & nature \ }; \end{tikzpicture} \end{frame}

\begin{frame} \begin{tikzpicture}[overlay,remember picture] \matrix at (current page.north) [shift={(-.5.85\textwidth,-2cm)}, allmatrix=north west, nodes={draw}, table/.list={1/width("le monde"), 2/width("new york times"), 3/width("penpavar"), 4/width("southern echo")}] (near-l) { le monde & new york times\ innadu & le maine \ times & spectrum \ }; \matrix at (current page.north) [shift={(.5.85\textwidth,-2cm)}, allmatrix=north east, nodes={draw}, table/.list={1/width("penpavar"), 2/width("southern echo")}] (near-r) { le figaro & southern echo\ penpavar & the hindu \ science & nature \ }; \end{tikzpicture} \end{frame} \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
  • Of course, the third column that is originally though to be the spacer can just have an empty path of horizontal length of \pgfkeysvalueof{/tikz/user1850133 column sep} but setting the column sep directly seems more natural. – Qrrbrbirlbel Nov 16 '22 at 14:47
  • your solutions and your explanations rock, though i do not get exactly what you have. – user1850133 Nov 19 '22 at 07:18
  • here the output i get for both frames: https://imgur.com/a/rNQSJMb – user1850133 Nov 19 '22 at 07:45
  • @user1850133 I can't reproduce this. Are you using an up-to-date TeX installation? It looks like some widths are evaluated by PGFmath to be slightly smaller than when TeX later builds a box. Did you change any font settings? – Qrrbrbirlbel Nov 19 '22 at 13:00
  • Though it should work with any font, the font, as displayed in the log at the end, is lmsans10-regular.otf. I copy-pasted your code without any change. I use lualatex (LuaHBTeX) 1.15.0. Tex Live 2022 (2022.8.12). Both lualatex and pdflatex give the same result. – user1850133 Nov 19 '22 at 14:42
  • @user1850133 Interesting, I get similar problems with LuaLaTeX but not with pdfLaTeX. This needs more research (possibly a PGFMath bug) but for a quick fix you could just add 1pt or any other small enough value to the width of the node (and as well as a substraction to user1850133 column sep): text width=#2+1pt and user1850133 column sep/.append={-#2-1pt-2*\pgfkeysvalueof{/pgf/inner xsep}}. – Qrrbrbirlbel Nov 19 '22 at 14:53
3

Something like this?

\documentclass{beamer}
\newlength{\slw}\setlength{\slw}{160mm}
\newlength{\slh}\setlength{\slh}{100mm}
\geometry{verbose,papersize={\slw,\slh}}

\usepackage{tikz,calc,xfp} \usetikzlibrary{calc,matrix} \tikzset{ allmatrix/.style = {matrix of nodes, rounded corners = 1mm, nodes in empty cells,matrix anchor=#1, row sep=-\pgflinewidth, column sep=-\pgflinewidth, ampersand replacement=&, nodes={outer sep=0pt}, text height=1.5ex, text depth=.25ex}, }

\begin{document} \begin{frame} \begin{tikzpicture}[overlay,remember picture]

\matrix at (current page.north) [shift={(0,-2)}, allmatrix=north, column 1/.append style={nodes={align=left,draw, text width={width("le monde")}, }}, column 2/.append style={nodes={align=left,draw, text width={width("new york times")}, }}, column 3/.append style={nodes={align=left,text width={.08\paperwidth}, }}, column 4/.append style={nodes={align=left,draw, text width={width("penpavar")}, }}, column 5/.append style={nodes={align=left,draw, text width={width("southern echo")}, }}] (mytab) % table/.list={1/width("le monde"), % 2/width("new york times"), % 4/width("le figaro"), % 5/width("southern echo")}] (near) { le monde & new york times & & le figaro & southern echo\ innadu & le maine & & penpavar & the hindu \ times & spectrum & & science & nature \ }; \end{tikzpicture} \end{frame} \end{document}

enter image description here

CarLaTeX
  • 62,716
3

Just a suggestion. As OPs uses absolut positioning, I think it's easier to place two independent matrices than computing the distance between columns. This is the code for my approach using CarLaTeX code.

\documentclass{beamer}

\usepackage{tikz,calc,xfp} \usetikzlibrary{calc,matrix} \tikzset{ allmatrix/.style = {matrix of nodes, rounded corners = 1mm, nodes in empty cells,matrix anchor=#1, row sep=-\pgflinewidth, column sep=-\pgflinewidth, ampersand replacement=&, nodes={outer sep=0pt}, text height=1.5ex, text depth=.25ex}, nodes={align=left, draw} }

\begin{document} \begin{frame} \begin{tikzpicture}[overlay,remember picture]

\matrix at (current page.north west) [shift={(1.5,-2)}, allmatrix=north west, draw=none, column 1/.append style={nodes={text width={width("le monde")}}}, column 2/.append style={nodes={text width={width("new york times")}}}] (mytab1) { le monde & new york times\ innadu & le maine\ times & spectrum\ };

\matrix at (current page.north east) [shift={(-1.5,-2)}, allmatrix=north east, draw=none, column 1/.append style={nodes={text width={width("penpavar")}}}, column 2/.append style={nodes={text width={width("southern echo")}}}] (mytab2) { le figaro & southern echo\ penpavar & the hindu \ science & nature \ }; \end{tikzpicture} \end{frame} \end{document}

enter image description here

Ignasi
  • 136,588