4

I'd like to perfect my linejoins. How can I accomphish this? I tried line join=round in the \begin{tikzpicture} environment, like so \begin{tikzpicture}[line join=round], but it didn't solve the issue.

top right enter image description here

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
%\usepackage[graphics, active, tightpage]{preview}
%\PreviewEnvironment{tikzpicture}

%!tikz preamble begin
\usetikzlibrary{shapes.geometric, arrows.meta, 3d, calc}
\usepackage{tikz-3dplot}

\usetikzlibrary{decorations.markings}

\usetikzlibrary{shapes,positioning, backgrounds, scopes}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground} 
%!tikz preamble end

\begin{document}
\begin{tikzpicture}[>=latex,line join=round,scale=1.5]

\pgfmathsetmacro{\cubex}{5}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{5}
\newcommand{\boxcolor}{yellow!20!}
\begin{pgfonlayer}{foreground}
    \draw[red!20!] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \draw[red!20!] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \draw[red!20!] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{pgfonlayer}
\begin{pgfonlayer}{background}
    \draw[fill=\boxcolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \draw[fill=\boxcolor] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \draw[fill=\boxcolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{pgfonlayer}

\end{tikzpicture}
\end{document}

Thanks!

  • 2
    You could use the line cap=round option, but wihtout a compilabe example it may be hard to completely answer that question. line cap handles line endings, while line join handles connections between segments. As you use multiple \draw calls, you have issues at the line endings. – BambOo Sep 16 '19 at 14:42
  • I need layers because I have stuff inside the box, which I want behind the boarders and in front of background. (Not shown in code) – avermaet Sep 16 '19 at 14:46
  • 1
    You are drawing centered 2d lines in 3d space, so that so the outer edges are extending past the centers. – John Kormylo Sep 16 '19 at 14:50
  • What does this mean? How can I improve this? – avermaet Sep 16 '19 at 14:52
  • 1
    You can solve the issue by saying e.g. rounded corners=\pgflinewidth/2 (a smaller radius would also do), e.g. in the options of the tikzpicture : \begin{tikzpicture}[>=latex,line join=round,scale=1.5,rounded corners=\pgflinewidth/2] .... (I also do not understand why one needs layers here.) – Schrödinger's cat Sep 16 '19 at 16:31
3

An alternative version without \clip but also with pgfonlayers replaced with scopes. Note that the \draw[fill=\boxcolor] were replaced with \fill[\boxcolor]. Using \draw[fill=\boxcolor,line cap=round, line join =round]also works fine.

\documentclass[tikz,border=1cm]{standalone}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[>=latex,scale=1.5,line cap=round, line join =round]
\pgfmathsetmacro{\cubex}{5}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{5}
\newcommand{\boxcolor}{yellow!20!}
\begin{scope}
    \node at (-0.5*\cubex,-0.5*\cubey,-0.25*\cubez) {\Huge some text};
    \draw[red!20!] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \draw[red!20!] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \draw[red!20!] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;

\end{scope}
\begin{scope}[on background layer={line cap=round, line join =round}]
    \fill[\boxcolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \fill[\boxcolor] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \fill[\boxcolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{scope}

\end{tikzpicture}
\end{document}

EDIT Looking back at the pgf-tikz manual, §45 related to the backgrounds library, it is explained that the options passed to on background layer allow to pass options to all the objects inside the scope, which solves the problem completely.

enter image description here

  • What's the difference of \fill[...] to \draw[fill=...] or scopeto pgfonlayer so that this works? – avermaet Sep 16 '19 at 16:04
  • 1
    \draw[fill=...] draws black lines and fills the area. \fill[...] only fills the area. Note that you probably meant \path[fill=...] which is equivalent to \fill[...]. The scope does not make much difference here but it avoids setting layers explicitely – BambOo Sep 16 '19 at 16:31
3

You can use clip to only draw the inside half of lines along the edges. Of course, lines internal to the clip will be twice as wide.

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
%\usepackage[graphics, active, tightpage]{preview}
%\PreviewEnvironment{tikzpicture}

%!tikz preamble begin
\usetikzlibrary{shapes.geometric, arrows.meta, 3d, calc}
\usepackage{tikz-3dplot}

\usetikzlibrary{decorations.markings}

\usetikzlibrary{shapes,positioning, backgrounds, scopes}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground} 
%!tikz preamble end

\begin{document}
\begin{tikzpicture}[>=latex,line join=round,scale=1.5]

\pgfmathsetmacro{\cubex}{5}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{5}
\newcommand{\boxcolor}{yellow!20!}

\begin{pgfonlayer}{foreground}
  \clip (-\cubex,0,-\cubez) -- (0,0,-\cubez) -- (0,-\cubey,-\cubez) -- (0,-\cubey,0)
    -- (-\cubex,-\cubey,0)-- (-\cubex,0,0) -- cycle;
    \draw[red!20!] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \draw[red!20!] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \draw[red!20!] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{pgfonlayer}
\begin{pgfonlayer}{background}
  \clip (-\cubex,0,-\cubez) -- (0,0,-\cubez) -- (0,-\cubey,-\cubez) -- (0,-\cubey,0)
    -- (-\cubex,-\cubey,0)-- (-\cubex,0,0) -- cycle;
    \draw[fill=\boxcolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
    \draw[fill=\boxcolor] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
    \draw[fill=\boxcolor] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{pgfonlayer}

\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.