Outils pour utilisateurs

Outils du site


pele_mele:stack_exchange:tex_latex:377681

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:377681 [2024/11/23 04:02] – supprimée - modification externe (Date inconnue) 127.0.0.1pele_mele:stack_exchange:tex_latex:377681 [2024/11/23 04:02] (Version actuelle) – ↷ Nom de la page changé de pele_mele:stack_exchange:tex_latex:tex-377681 à pele_mele:stack_exchange:tex_latex:377681 alexis
Ligne 1: Ligne 1:
 +====== How to keep cell border with a column background color? ======
 +I used my [[https://tex.stackexchange.com/questions/367891/is-there-a-better-way-for-drawing-my-table|score board]] for a while now, but it appears that it's hard to know in which column I need to write.
 +
 +So my idea was to color every other column so I know which one I need to use.
 +
 +Here is what I got so far:
 +
 +<code latex>
 +\documentclass{letter}
 +
 +\usepackage[a4paper, landscape, margin=0.5cm]{geometry}
 +\usepackage{multirow}
 +\usepackage{tabularx}
 +\usepackage{graphicx}
 +\usepackage{color, colortbl}
 +
 +\setlength{\extrarowheight}{14pt}
 +
 +\definecolor{Gray}{gray}{0.85}
 +
 +\newcolumntype{g}{>{\columncolor{Gray}}c}
 +\newcolumntype{G}{>{\columncolor{Gray}}X}
 +
 +\newcommand\customTable{
 +    \begin{tabularx}{\textwidth}{cc|c|X|g|G|c|X|g|G|c|X|g|G|c|X|g|G|c|X|g|G|c|c|}
 +        \cline{3-22}
 +
 +        & & \multicolumn{20}{ c| }{\LARGE Joueurs} \\ \cline{3-22}
 +
 +        & & \multicolumn{4}{ c| }{} & \multicolumn{4}{ c| }{} & \multicolumn{4}{ c| }{} & \multicolumn{4}{ c| }{} & \multicolumn{4}{ c| }{} \\ \cline{1-24}
 +
 +        \customRow{10}{\multirow{20}{*}{\rotatebox[origin=c]{90}{\LARGE Cartes}}}\cline{2-24}
 +        \customRow{9}{}\cline{2-24}
 +        \customRow{8}{}\cline{2-24}
 +        \customRow{7}{}\cline{2-24}
 +        \customRow{6}{}\cline{2-24}
 +        \customRow{5}{}\cline{2-24}
 +        \customRow{4}{}\cline{2-24}
 +        \customRow{3}{}\cline{2-24}
 +        \customRow{2}{}\cline{2-24}
 +        \customRow{1}{}\cline{1-22}
 +    \end{tabularx}
 +}
 +
 +\newcommand\customHeader[2]{
 +    \multicolumn{1}{|c}{#2} &
 +    \multicolumn{1}{|c|}{\multirow{2}{*}{\LARGE #1}}
 +}
 +
 +\newcommand\emptyCells{
 +    & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & ~~~ \\
 +}
 +
 +\newcommand\customBorder{
 +    \cline{3-3} \cline{5-5} \cline{7-7} \cline{9-9} \cline{11-11} \cline{13-13} \cline{15-15} \cline{17-17} \cline{19-19} \cline{21-21} \cline{23-24}
 +}
 +
 +\newcommand\emptyDoubleCells{
 +    \multicolumn{1}{|c}{} & \multicolumn{1}{|c|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{g|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{g|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{g|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{g|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{g|}{} \\
 +}
 +
 +\newcommand\customRow[2]{
 +    \customHeader{#1}{#2}\emptyCells\customBorder\emptyDoubleCells
 +}
 +
 +\begin{document}
 +    \pagenumbering{gobble}
 +    \customTable
 +    \customTable
 +\end{document}
 +</code>
 +
 +Now, every other column have a background but I've lost the borders on those.
 +
 +I've looked [[http://texblog.org/2011/04/19/highlight-table-rowscolumns-with-color/|here]] and [[https://tex.stackexchange.com/questions/94799/how-do-i-color-table-columns|here]] for inspiration, but I cannot spot where is my mistake.
 +
 +So how do I color the background and keep the borders at the same time?
 +
 +<WRAP help>
 +The trick in case of problems with lines in coloured tables consists in using ''hhline'', so I redefined ''\customBorder''. Note the very thin white lines in the coloured cells seem to come from the viewer.
 +
 +Some explanations on the code:
 +
 +An ''\hhline'' is described by a succession of symbols (one per column): either ''~'' (no rule in the corresponding column), or ''-'' (a single rule) or ''='' (a double rule) These symbols can be separated by symbols describing how horizontal rule and vertical rule intersect (''|'', ''||'' and '':''). Furthermore, if a motto is repeated several times, a short syntax similar to the ''array'' package syntax for repeated column type can be used. Here, I used ''*{5}{...}'', which means the same motto is repeated 4 times (and used 5!). Also the syntax ''>{...}'' allows to insert code before the following segment (e.g. to change the rule colour from the next segment).
 +
 +To delete white rules in coloured cells, I replaced ''~'' which would have been used if the cell were not coloured, with a rule segment of the same colour as the cell, like this ''|>{\arrayrulecolor{Gray}->{\arrayrulecolor{black}|- ...''.
 +
 +<code latex>
 +\documentclass{letter}
 +
 +\usepackage[a4paper, landscape, margin=0.5cm]{geometry}
 +\usepackage{multirow}
 +\usepackage{tabularx}
 +\usepackage{graphicx}
 +\usepackage[table]{xcolor}
 +\usepackage{hhline, booktabs}
 +\newcommand\mycline{\hhline{|~*{23}{|-}}}
 +
 +\setlength{\extrarowheight}{14pt}
 +
 +\definecolor{Gray}{gray}{0.85}
 +
 +\newcolumntype{g}{>{\columncolor{Gray}}c}
 +\newcolumntype{G}{>{\columncolor{Gray}}X}
 +
 +\newcommand\customTable{
 +    \begin{tabularx}{\textwidth}{cc|*{5}{c|X|g|G|}c|c|}
 +        \cline{3-22}
 +
 +        & & \multicolumn{20}{ c| }{\LARGE Joueurs} \\ \cline{3-22}
 +
 +        & & \multicolumn{4}{ c| }{} & \multicolumn{4}{ c| }{} & \multicolumn{4}{ c| }{} & \multicolumn{4}{ c| }{} & \multicolumn{4}{ c| }{} \\ %\cline{1-24}
 +\hhline{*{24}{-}}
 +
 +        \customRow{10}{\multirow{20}{*}{\rotatebox[origin=c]{90}{\LARGE Cartes}}}%\cline{2-24}
 +\mycline
 +        \customRow{9}{}\mycline
 +        \customRow{8}{}\mycline
 +        \customRow{7}{}\mycline
 +        \customRow{6}{}\mycline
 +        \customRow{5}{}\mycline
 +        \customRow{4}{}\mycline
 +        \customRow{3}{}\mycline
 +        \customRow{2}{}\mycline
 +        \customRow{1}{}\cline{1-22}
 +    \end{tabularx}
 +}
 +
 +\newcommand\customHeader[2]{
 +    \multicolumn{1}{|c|}{#2} &
 +\multicolumn{1}{c|}{\multirow{2}{*}{\LARGE #1}}
 +}
 +
 +\newcommand\emptyCells{
 +    & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & ~~~ \\
 +}
 +
 +\newcommand\customBorder{%
 +\hhline{|~|~|*{5}{>{\arrayrulecolor{black}}-~|->{\arrayrulecolor{Gray}}->{\arrayrulecolor{black}}|}--}
 +}%
 +
 +\newcommand\emptyDoubleCells{
 +    \multicolumn{1}{|c|}{} & \multicolumn{1}{c|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{g|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{g|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{g|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{g|}{} & \multicolumn{2}{c|}{} & \multicolumn{2}{g|}{} \\
 +}
 +
 +\newcommand\customRow[2]{
 +    \customHeader{#1}{#2}\emptyCells\customBorder\emptyDoubleCells
 +}
 +
 +\begin{document}
 +
 +    \pagenumbering{gobble}
 +    \customTable
 +    \customTable
 +
 +\end{document} 
 +</code>
 +
 +{{:pele_mele:stack_exchange:tex-377681-1.png?nolink|}}
 +</WRAP>
 +<WRAP help>
 +I learned how to deal with the annoying effect of white lines in colored cells using the technique explained in the comment of Ulrike Fischer in [[https://tex.stackexchange.com/questions/355974/hhline-and-cell-color-problems-revisited|hhline and cell color problems revisited]]
 +
 +It is only necessary to raise the line a little.
 +
 +In the above code, replace
 +
 +<code latex>
 +\newcommand\emptyCells{
 +    & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & ~~~ \\
 +}
 +</code>
 +
 +with
 +
 +<code latex>
 +\newcommand\emptyCells{
 +    & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & & ~~~ & ~~~ \\[-0.5pt] % small shift up <<<<<<<<
 +}
 +</code>
 +
 +It may just be a PDF viewer effect, but you never know what will come out of a digital press.
 +
 +Original output
 +
 +{{:pele_mele:stack_exchange:tex-377681-2.jpg?nolink|}}
 +
 +With the modified code
 +
 +{{:pele_mele:stack_exchange:tex-377681-3.jpg?nolink|}}
 +</WRAP>
 +<WRAP info>
 +[[https://tex.stackexchange.com/questions/377681/how-to-keep-cell-border-with-a-column-background-color|tables - How to keep cell border with a column background color? - TeX - LaTeX Stack Exchange]]
 +</WRAP>