Outils pour utilisateurs

Outils du site


pele_mele:stack_exchange:tex_latex:392900

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
pele_mele:stack_exchange:tex_latex:392900 [2024/11/23 04:02] – supprimée - modification externe (Date inconnue) 127.0.0.1pele_mele:stack_exchange:tex_latex:392900 [2024/11/23 04:02] (Version actuelle) – ↷ Nom de la page changé de pele_mele:stack_exchange:tex_latex:tex-392900 à pele_mele:stack_exchange:tex_latex:392900 alexis
Ligne 1: Ligne 1:
 +====== How positioning works with tikz ======
 +I am back again with [[https://tex.stackexchange.com/questions/367891/is-there-a-better-way-for-drawing-my-table|my table]].
 +
 +I've added a tikz picture on the right side. I am quite happy with the result but I don't truly understand what I've done since I copied some code from here and tweaked it a little bit.
 +
 +Here is the code:
 +
 +<code latex>
 +\documentclass{letter}
 +
 +\usepackage[a4paper, margin=15pt]{geometry}
 +\usepackage{multirow}
 +\usepackage{tabularx}
 +\usepackage{graphicx}
 +\usepackage[utf8]{inputenc}
 +\usepackage{tikz}
 +
 +\newlength{\Oldarrayrulewidth}
 +\newcommand{\Cline}[2]{%
 +  \noalign{\global\setlength{\Oldarrayrulewidth}{\arrayrulewidth}}%
 +  \noalign{\global\setlength{\arrayrulewidth}{#1}}\cline{#2}%
 +  \noalign{\global\setlength{\arrayrulewidth}{\Oldarrayrulewidth}}}
 +
 +\newcommand\VRule{\vrule width 2pt}
 +
 +\newcommand\mottonScoreHeader[2]{%
 +    \multicolumn{1}{|c}{\textbf{#2}} &
 +    \multicolumn{1}{|c|}{\multirow{2}{*}{\textbf{#1}}}
 +}
 +
 +\newcommand\mottonScoreEmptyCells{%
 +    & \multicolumn{1}{c!{\VRule}}{} & & & & & & & & & & & & & & &\\
 +}
 +
 +\newcommand\mottonScoreBorder{%
 +    \cline{4-4} \cline{7-7} \cline{10-10} \cline{13-13} \cline{16-16}
 +}
 +
 +\newcommand\mottonScoreEmptyDoubleCells{%
 +    \multicolumn{1}{|c}{} & \multicolumn{1}{|c|}{} & \multicolumn{1}{|c!{\VRule}}{} & & \multicolumn{2}{c!{\VRule}}{} & & \multicolumn{2}{c!{\VRule}}{} & & \multicolumn{2}{c!{\VRule}}{} & & \multicolumn{2}{c!{\VRule}}{} & & \multicolumn{2}{c!{\VRule}}{}\\
 +}
 +
 +\newcommand\mottonScoreRow[2]{%
 +    \mottonScoreHeader{#1}{#2}\mottonScoreEmptyCells\mottonScoreBorder\mottonScoreEmptyDoubleCells
 +}
 +
 +\newcommand\mottonScore{%
 +    \setlength{\extrarowheight}{2pt}
 +    \begin{tabularx}{\textwidth}{ccp{15pt}|*{5}{p{8pt}|X@{+}p{15pt}!{\VRule}}}
 +        \cline{4-18}
 +
 +        & & & \multicolumn{15}{c|}{\textbf{Joueurs}} \\ \cline{4-18}
 +
 +        & & \multicolumn{1}{c!{\VRule}}{} & \multicolumn{3}{c!{\VRule}}{} & \multicolumn{3}{c!{\VRule}}{} & \multicolumn{3}{c!{\VRule}}{} & \multicolumn{3}{c!{\VRule}}{} & \multicolumn{3}{c!{\VRule}}{} \\ \cline{1-18}
 +
 +        \mottonScoreRow{10}{\multirow{40}{*}{\rotatebox[origin=c]{90}{Cartes}}}\cline{2-18}
 +        \mottonScoreRow{9}{}\cline{2-18}
 +        \mottonScoreRow{8}{}\cline{2-18}
 +        \mottonScoreRow{7}{}\cline{2-18}
 +        \mottonScoreRow{6}{}\cline{2-18}
 +        \mottonScoreRow{5}{}\cline{2-18}
 +        \mottonScoreRow{4}{}\cline{2-18}
 +        \mottonScoreRow{3}{}\cline{2-18}
 +        \mottonScoreRow{2}{}\cline{2-18}
 +        \mottonScoreRow{1}{}\Cline{2pt}{2-18}
 +        \mottonScoreRow{1}{}\cline{2-18}
 +        \mottonScoreRow{2}{}\cline{2-18}
 +        \mottonScoreRow{3}{}\cline{2-18}
 +        \mottonScoreRow{4}{}\cline{2-18}
 +        \mottonScoreRow{5}{}\cline{2-18}
 +        \mottonScoreRow{6}{}\cline{2-18}
 +        \mottonScoreRow{7}{}\cline{2-18}
 +        \mottonScoreRow{8}{}\cline{2-18}
 +        \mottonScoreRow{9}{}\cline{2-18}
 +        \mottonScoreRow{10}{}\cline{1-18}
 +    \end{tabularx}
 +}
 +
 +\newcommand\mottonDataHeader[2]{%
 +    \multicolumn{1}{|c}{#2} & \multicolumn{1}{|c|}{\textbf{#1}}
 +}
 +
 +\newcommand\mottonData{%
 +    \setlength{\extrarowheight}{3pt}
 +    \begin{tabular}{cc|*{11}{c|}}
 +        \cline{3-13}
 +        & & \multicolumn{11}{c|}{\textbf{Levées réalisées}} \\
 +        \cline{3-13}
 +        & & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} & \textbf{8} & \textbf{9} & \textbf{10} \\
 +        \cline{1-13}
 +        \mottonDataHeader{10}{\multirow{10}{*}{\rotatebox[origin=c]{90}{\textbf{Cartes en main}}}}
 +        & 10 & 11 & 14 & 19 & 26 & 35 & 46 & 59 & 74 & 91 & 110 \\
 +        \cline{2-13}
 +        \mottonDataHeader{9}{}
 +        & 10 & 12 & 16 & 22 & 30 & 40 & 52 & 66 & 82 & 100 \\
 +        \cline{2-12}
 +        \mottonDataHeader{8}{}
 +        & 10 & 13 & 18 & 25 & 34 & 45 & 58 & 73 & 90 \\
 +        \cline{2-11}
 +        \mottonDataHeader{7}{}
 +        & 10 & 14 & 20 & 28 & 38 & 50 & 64 & 80 \\
 +        \cline{2-10}
 +        \mottonDataHeader{6}{}
 +        & 10 & 15 & 22 & 31 & 42 & 55 & 70 \\
 +        \cline{2-9}
 +        \mottonDataHeader{5}{}
 +        & 10 & 16 & 24 & 34 & 46 & 60 \\
 +        \cline{2-8}
 +        \mottonDataHeader{4}{}
 +        & 10 & 17 & 26 & 37 & 50 \\
 +        \cline{2-7}
 +        \mottonDataHeader{3}{}
 +        & 10 & 18 & 28 & 40 \\
 +        \cline{2-6}
 +        \mottonDataHeader{2}{}
 +        & 10 & 19 & 30 \\
 +        \cline{2-5}
 +        \mottonDataHeader{1}{}
 +        & 10 & 20 \\
 +        \cline{1-4}
 +    \end{tabular}
 +    \mottonTitle
 +    \[
 +    score = (10 + lev\acute{e}es\ r\acute{e}alis\acute{e}es - cartes\ en\ main) \times lev\acute{e}es\ r\acute{e}alis\acute{e}es + 10
 +    \]
 +}
 +
 +\newcommand{\mottonTitle}[1][]{%
 +    \begin{tikzpicture}[#1]
 +        \node[text width=9cm,align=center](titre){\Huge Motton};
 +        \node[text width=9cm,align=center] at ([yshift=-1cm]titre.north){Feuille de score};
 +    \end{tikzpicture}
 +
 +
 +\begin{document}
 +    \pagenumbering{gobble}
 +    \mottonData
 +
 +    \mottonScore
 +
 +    \mottonData
 +
 +    \mottonScore
 +\end{document}
 +</code>
 +
 +My questions are:
 +
 +  * Why my tikz picture is vertically aligned with the middle of the table? I was expecting it would be aligned from the top
 +  * Why do I need to set the tikz picture width if I want to center it? Can't I just position the picture where I want?
 +
 +It seems that there are some misunderstandings, so I'll try to add some clarifications. I am happy with what I've got. I just don't understand how I've got it.
 +
 +From what I've coded, I thought the picture would be aligned on the top of the table. I was planning to move it down later. But it was already located where I wanted. That's the topic of the first question.
 +
 +For the second question, if I don't set the width of the node, the node is on the left side, aligned to the edge of the table. My guess is because the node takes only the space needed to display the text contained. Am I right?
 +
 +<WRAP help>
 +To understand what happens, consider this simplified example:
 +
 +<code latex>
 +\documentclass{article}
 +\usepackage{tikz}
 +\begin{document}
 +X
 +\begin{tabular}{c} 1 \\ 2 \\3 \\4 \end{tabular}
 +\begin{tikzpicture}
 +\node [align=center] {Motton\\ Foobar};
 +% the following line draws a frame around the border of the diagram
 +\draw (current bounding box.north east) rectangle (current bounding box.south west);
 +\end{tikzpicture}
 +X
 +\end{document}
 +</code>
 +
 +The output is this:
 +
 +{{:pele_mele:stack_exchange:tex-392900-1.png?nolink|}}
 +
 +There are a few things to note:
 +
 +  * The default vertical alignment of a ''tabular'' is to center it on the baseline.
 +  * The default vertical aligntment of a ''tikzpicture'' is to place its bottom on the baseline.
 +  * A ''tikzpicture'' is cropped to its contents.
 +  * As far as positioning on the line/page, a ''tikzpicture'' is just a box, and in that sense no different from say the letter X, or a ''tabular'', or an ''\includegraphics''.
 +
 +The first two points is the reason why your title roughly aligned with the center of the tabular.
 +
 +Regarding the horizontal positioning, the last two points are the reason you need ''text width=9cm''. That setting makes the node wider, but because the text is centered within that 9cm, the text is pushed to the right of the table. For example, if I add ''text width=5cm'' to the ''node'' in my example above, I get this:
 +
 +{{:pele_mele:stack_exchange:tex-392900-2.png?nolink|}}
 +
 +But to push something a bit right on a line, it's perhaps more natural to insert some horizontal space, so you could add e.g. ''\hspace{3cm}'' between ''\end{tabular}'' and ''\begin{tikzpicture}''.
 +</WRAP>
 +<WRAP help>
 +try
 +
 +<code latex>
 +\newcommand{\mottonTitle}[1][]{%
 +    \begin{tikzpicture}[#1,baseline=(current bounding box.north)]% for align to the top is added baseline 
 +        \node[text width=9cm,align=center](titre){\Huge Motton      \\
 +                                                  \normalsize  at   \\ 
 +                                                  Feuille de score};
 +    \end{tikzpicture}
 +}
 +</code>
 +
 +and to table (left of image with title) add optional table align ''[t]'':
 +
 +<code latex>
 +\begin{tabular}[t]{cc|*{11}{c|}}% added `t`
 +</code>
 +
 +{{:pele_mele:stack_exchange:tex-392900-3.png?nolink|}}
 +
 +do i correctly understood, what you like to obtain?
 +
 +**edit:** for aligning table (''tabular'' environment) and image (''tikzpicture'' environment) with title is important that both have the same baseline. with option ''[t]'' (top) the table is aligned to their top. similarly with defined baseline of ''tikzpicture'' to be at its north side (''current bounding box.north'') both baseline are in line. if you not define this, the natural baseline for table is middle of table and image is bottom of image.
 +</WRAP>
 +<WRAP info>
 +[[https://tex.stackexchange.com/questions/392900/how-positioning-works-with-tikz|How positioning works with tikz - TeX - LaTeX Stack Exchange]]
 +</WRAP>