4

I have defined a \newcommand {atest} to draw a line composed with a snowflake, as in the figure 1. To adjust the length of the composition to the right margin of the page I use two parameters and I have made the adjustment by trial and error. Obviously this is not a good solution.

On my second try, with \newcommand {btest}, I get the line length adjustment automatically but not the line thickness. I think it shouldn't be difficult to figure this out, but I'm still a latex beginner and almost everything I do is by trial and error.

\documentclass[10pt]{article}
  \usepackage{tikz}
  \usetikzlibrary{decorations.fractals}

\newcommand{\atest}[3]{% \begin{tikzpicture}[scale=3, line width=2pt, line cap=round, #3, decoration=Koch snowflake, baseline={(0,-0.05)}] \draw (0,0) -- (#1,0); \draw decorate{(#1,0) -- (#2,0)};
\end{tikzpicture} }

\newcommand{\btest}[1]{%
%parameter #1 is for line color \linewidth=3pt \color{#1} \hrulefill {\tikz[decoration=Koch snowflake, line cap=round]{\draw decorate{ (0,-2) -- (3,-2)};}}
}

\begin{document}
  \textbf{Solution 1.1} \atest{3.2}{4.2}{gray!50}

\ \textbf{Solution 1.1} \btest{gray!50}

\end{document}




Image 1: result with \textbf{Solution 1.1}
\atest{3.2}{4.2}{gray!50}

Image 2: result with \textbf{Solution 1.1} \btest{gray!50}

**Version 4 (based on "chsk" code): 4 parameters. Imperfect fit between \hline and snowflake **

    \documentclass[10pt]{article}
      \usepackage{tikz}
      \usetikzlibrary{decorations.fractals}
    \newcommand{\refdec}[4]{%    
        % parameter #1 is for thickness
        % parameter #2 is for line color
        % parameter #3 is for the number of iterations (e.g. 2)
        % parameter #4 is for vertical reflection: #4 = -1 -> reflection, #4 = 1 -> no reflection
        \def\kochpath{(0,-2) -- (3,-2)}%
        \foreach \n in {1,...,#3}{\xdef\kochpath{decorate{\kochpath}}}%
        {\color{#2}%                
            \leavevmode\leaders\hrule height #1\relax\hfill%
            \raisebox{1.5pt}{\scalebox{1}[#4]{
            \tikz[decoration=Koch snowflake, line cap=rect]{\draw[line width=#1] \kochpath;}}}%
        }%
    }
 \begin{document}
     \textbf{Solution 1.1} \refdec{2pt}{red}{3}{-1}
\\
    \textbf{Solution 1.2} \refdec{2pt}{red}{3}{1}
 \end{document}

enter image description here

1 Answers1

5

Version 1: adjusting color

\hrulefill defaults to a thickness of 0.4pt. Here's a snippet that defines a new command in the same vein, \thickhrulefill, that is 3pt thick instead:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}
\makeatletter
\newcommand{\thickhrulefill}{%
\leavevmode\leaders\hrule height 3pt\hfill\kern\z@}
\makeatother
\newcommand{\btest}[1]{%    
    %parameter #1 is for line color
    \color{#1}
    \thickhrulefill
    {\tikz[decoration=Koch snowflake, line cap=rect]{\draw[line width=3pt] decorate{ (0,-2) -- (3,-2)};}}  
}

\begin{document} \noindent\textbf{Solution 1.1} \btest{gray!50} \end{document}

enter image description here

I've also adjusted the line caps to line cap=rect to make sure there's no unsightly gaps.

For future reference: the (re-)implementation of \hrulefill was taken from \hrulefill & \dotfill on latexref.xyz.

You can also query the definitions of commands by e.g. latexdef -t latex \hrulefill on the command line (if you're on Windows, use cmd.exe for this), which will say

\hrulefill:
macro:->\protect \hrulefill

\hrulefill : macro:->\leavevmode \leaders \hrule \hfill \kern \z@

though that's admittedly not so helpful in this particular case since there's no indication of what \hrule's default thickness is or how to change it. (Still useful in general though!)

Version 2: adjust color and thickness

EDIT: here's a version that allows you to set both line color and thickness:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}
\newcommand{\btest}[2]{%    
    % parameter #1 is for thickness
    % parameter #2 is for line color
    {\color{#2}
    \leavevmode\leaders\hrule height #1\relax\hfill
    {\tikz[decoration=Koch snowflake, line cap=rect]{\draw[line width=#1] decorate{ (0,-2) -- (3,-2)};}}}
}

\begin{document} \noindent\textbf{Solution 1.1} \btest{1pt}{blue!50}

\noindent\textbf{Task1.2} \btest{3pt}{gray!50}

\noindent\textbf{Hint 1.3} \btest{5pt}{red!50}

\end{document}

enter image description here

I've actually gotten rid of the \kerning (\kern\z@) that would usually appear after the \hfill here; I don't think it's needed; and I've enclosed the whole thing in a group to make sure the \color change is local and will not affect subsequent text.

Version 3: adjusting color, thickness and number of snowflake iterations

EDIT 2: as requested, here's a version that allows you to also specify the number of iterations:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals}

\newcommand{\ctest}[3]{%
% parameter #1 is for thickness (e.g. 1pt) % parameter #2 is for line color (e.g. blue!50) % parameter #3 is for the number of iterations (e.g. 2) \def\kochpath{(0,-2) -- (3,-2)}% \foreach \n in {1,...,#3}{\xdef\kochpath{decorate{\kochpath}}}% {\color{#2}% \leavevmode\leaders\hrule height #1\relax\hfill% \tikz[decoration=Koch snowflake, line cap=rect]{\draw[line width=#1] \kochpath;}% }% }

\begin{document} \noindent\textbf{Solution 1.1} \ctest{3pt}{blue!50}{1}

\noindent\textbf{Task 1.2} \ctest{2pt}{gray!50}{2}

\noindent\textbf{Hint 1.3} \ctest{1pt}{red!50}{3}

\end{document}

enter image description here

This works by first defining the initial path, and then using a \foreach loop to successively wrap it in a number of decorate{ ... } statements. (For what \xdef does vs. \def, see egreg's excellent explanation here.)

Version 4: adjusting color, thickness and number of snowflake iterations, and allowing mirroring

EDIT 3: I've tried to also implement the ability to mirror the snowflake curve that @Jesús Álvarez Lobo suggested in a comment:

\documentclass[10pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.fractals,calc}

\newcommand{\ctest}[4][false]{%
% parameter #1 is for optional mirroring (i.e. true or false, default: false) % parameter #2 is for thickness (e.g. 1pt) % parameter #3 is for line color (e.g. blue!50) % parameter #4 is for the number of iterations (e.g. 2) \def\kochpath{(0,-2) -- (3,-2)}% \foreach \n in {1,...,#4}{\xdef\kochpath{decorate{\kochpath}}}% {\color{#3}% \leavevmode\leaders\hrule height #2\relax\hfill% \tikz[baseline={($(0,-2) - 0.5*(0,#2)$)}, decoration={Koch snowflake, mirror=#1}, line cap=rect]{\draw[line width=#2] \kochpath;}% }% }

\begin{document} \noindent\textbf{Solution 1.1} \ctest{3pt}{blue!50}{1}

\noindent\textbf{Task 1.2} \ctest{2pt}{gray!50}{2}

\noindent\textbf{Hint 1.3} \ctest[true]{1pt}{red!50}{3}

\end{document}

enter image description here

TikZ already includes the ability to mirror a decoration, by using decoration={..., mirror} (see section 24.4.1 of the massive TikZ manual), but when doing so the picture must also be adjusted so that the Koch curve is flush with the preceding \hrule. I've used the baseline option on the tikzpicture environment here, and performed the necessary calculation using the calc TikZ library. Mirroring is controlled by an optional parameter that precedes the rest (this is due to the way \newcommand works).

Speaking of adjustments, I have no idea how much vertical space any of the above versions generate above or below, or how much they should generate --- this can and should be investigated, I guess, but since this has really just been an opportunity for me to learn a little bit more about LaTeX and TikZ for me, I'll leave that to those who want to use these snippets in their documents.

chsk
  • 3,667
  • Thank you very much, chsk. You have provided an improvement, but we still have to resolve to implement the possibility of changing the line thickness. I have tried the following code but I get the error "missing number, treated as zero": \makeatletter \newcommand{\thickhrulefill}[1]{% \leavevmode\leaders\hrule height #1pt\hfill\kern\z@} \makeatother \newcommand{\btest}[2]{%
    %parameter #1 is for line color \color{#2} \thickhrulefill[#1] {\tikz[decoration=Koch snowflake, line cap=rect]{\draw[line width=#1] decorate{ (0,-2) -- (3,-2)};}}
    }
    – Jesús Álvarez Lobo Apr 14 '21 at 21:54
  • I get the error when I run \textbf{Solution 1.1} \btest{2}{gray!90} – Jesús Álvarez Lobo Apr 14 '21 at 21:59
  • 1
    @JesúsÁlvarezLobo my bad, I didn't realize you wanted to be able to change the line width on the fly as well. I've edited my post with an updated version (also slightly improved otherwise) that allows you to do that. – chsk Apr 15 '21 at 05:59
  • Thanks, chsk. Great solution !. I will reference you in the Latex code that I am writing for a book. If you allow me, since I see that you have a high level in Latex, could we implement to this command the possibility of parameterizing the number of iterations of the snowflake? Here is an example for 3 iterations in the fractal snowflake: – Jesús Álvarez Lobo Apr 15 '21 at 09:41
  • \newcommand{\ctest}[2]{%
    % parameter #1 is for thickness % parameter #2 is for line color {\color{#2} \leavevmode\leaders\hrule height #1\relax\hfill {\tikz[decoration=Koch snowflake, line cap=rect]{\draw[line width=#1] decorate{decorate{decorate{(0,-2) -- (3,-2)}}};}}} }
    – Jesús Álvarez Lobo Apr 15 '21 at 09:41
  • We want to add a parameter #3 for the iterations in decorate{decorate{decorate{... – Jesús Álvarez Lobo Apr 15 '21 at 09:43
  • 1
    @JesúsÁlvarezLobo Thank you! That's very kind of you. Parametrizing the number of iterations may be beyond my capabilities, though I'll see if I can figure something out. – chsk Apr 15 '21 at 15:41
  • Thank you!. Do not worry; it does not matter. I have only raised it in order to advance day by day in the knowledge of Latex. If you find it difficult, it may not be worth spending more time ... – Jesús Álvarez Lobo Apr 15 '21 at 16:04
  • 1
    @JesúsÁlvarezLobo See the updated version of my answer, I figured something out. And It's precisely because I found it difficult that it was worth spending time on. I'm still very much a journeyman myself, so every opportunity to learn something new is good. – chsk Apr 15 '21 at 16:11
  • Congratulations; you've got it!. Well, this is turning out to be a very didactic exercise, brief but involving many concepts. I have tried to go a little further, adding the possibility of vertically reflecting the snowflake. The result that I have obtained, although it works, is very imperfect. This is the code and images of the results: – Jesús Álvarez Lobo Apr 16 '21 at 11:55
  • \newcommand{\refdec}[4]{%
    % parameter #1 is for thickness % parameter #2 is for line color % parameter #3 is for the number of iterations (e.g. 2) % parameter #4 is for vertical reflection: #3 = -1 -> reflection, #3 = 1 -> no reflection \def\kochpath{(0,-2) -- (3,-2)}% \foreach \n in {1,...,#3}{\xdef\kochpath{decorate{\kochpath}}}% {\color{#2}%
    \leavevmode\leaders\hrule height #1\relax\hfill% \raisebox{1.5pt}{\scalebox{1}[#4]{ \tikz[decoration=Koch snowflake, line cap=rect]{\draw[line width=#1] \kochpath;}}}% }% }
    – Jesús Álvarez Lobo Apr 16 '21 at 11:57
  • \textbf{Solution 1.1} \refdec{2pt}{red}{3}{-1} \

    \textbf{Solution 1.2} \refdec{2pt}{red}{3}{1}

    – Jesús Álvarez Lobo Apr 16 '21 at 11:58
  • Code and image in an EDIT of my post, at the top of page. – Jesús Álvarez Lobo Apr 16 '21 at 12:20
  • 1
    Nice! I've tried my hand at this as well, see my latest edit to my answer. – chsk Apr 16 '21 at 18:32
  • Marvelous! A very valuable contribution. I no longer dare to continue testing you ... Thank you very much. Best regards from Spain. – Jesús Álvarez Lobo Apr 16 '21 at 19:41
  • 1
    @JesúsÁlvarezLobo Heh, thank you! All the best to you, and let me know when your book comes out, too! – chsk Apr 16 '21 at 19:54
  • Ok!. Of course, I will let you know. – Jesús Álvarez Lobo Apr 16 '21 at 20:01