
Ich habe große Probleme bei der Verwendung \ifodd
. Ich möchte ein Muster aus abwechselnd dicken und dünnen Linien erstellen. Ich habe mehrere Möglichkeiten ausprobiert, aber keine davon funktioniert.
Mit dieser Struktur
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[remember picture,overlay,shorten >= -10pt]
\coordinate (aux1) at ([xshift=-25mm, yshift=-30mm]current page.north east);
\coordinate (aux2) at ([xshift=-25mm, yshift=30mm]current page.south east);
\coordinate (aux3) at ([xshift=25mm, yshift=-30mm]current page.north west);
\coordinate (aux4) at ([xshift=25mm, yshift=30mm]current page.south west);
\begin{scope}[black]
\foreach \i [evaluate =\i as \x using int(\i)] in {0, ..., 4}{
\draw["\ifodd \x line width=1pt \else line width=2pt \fi" ]([
xshift=\x mm,
yshift=-65mm
]aux1)
--
([
xshift=\x mm,
yshift=10mm]
aux2);
}
\end{scope}
\end{tikzpicture}
\end{document}
Ich habe versucht
\foreach \x in {0, ..., 4}{
\draw["\ifodd \x line width=1pt \else line width=2pt \fi" ]([
}
\foreach \x in {0, ..., 4}{
\draw["\ifodd \num{\x} line width=1pt \else line width=2pt \fi" ]([
}
\foreach \x in {0, ..., 4}{
\draw["\ifodd \value{\x} line width=1pt \else line width=2pt \fi" ]([
}
\foreach \i [evaluate =\i as \x using int(\i)] in {0, ..., 4}{
\draw["\ifodd \x line width=1pt \else line width=2pt \fi" ]([
}
Hinweis: Ich möchte lernen, wie man \ifodd in Schleifen verwendet
Antwort1
Ihr Code weist mehrere Probleme auf. Das erste ist die Verwendung der Dokumentklasse „Standalone“ mit einem Knoten, der relativ zur Ecke der aktuellen Seite positioniert ist (bei Standalone ist die Seitengröße jedoch nicht festgelegt). Dies führt zu einem Fehler „Größe zu groß“ (kommentieren Sie die Zeile, die mit beginnt \draw
, und Sie erhalten ein sehr großes Dokument: 12,13 cm Breite und575.84 cmHöhe!)
Also ersetzen wir zunächsteigenständigevonArtikel.
Kleines Problem (nicht blockierend), die Koordinaten „aux3“ und „aux4“ werden nicht verwendet, also löschen wir sie.
Das zweite Problem betrifft die Syntax in der Zeile \draw["\ifodd \x line width=1pt \else line width=2pt \fi" ]([
.
Vergessen Sie zunächst die Anführungszeichen "
, das ist eine falsche Syntax.
Zweitens, wenn Sie es nach dem Entfernen versuchen "
, \ifodd \x line width=1pt \else line width=2pt \fi
funktioniert es immer noch nicht.
Aber \ifodd \x red\else blue\fi
funktioniert.
Wenn Sie Klammern hinzufügen umwahrer CodeUndfalscher Code, d. h. \ifodd \x{line width=1pt}\else{line width=2pt}\fi
Sie erhalten Fehlermeldungen wieIch kenne den Schlüssel „/tikz/line width=2pt“ nicht und werde ihn ignorieren.So TikZ sehen line width=1pt
alsSchlüssel, nicht so wieSchlüssel=Wert.
Das Problem scheint mit dem Zeichen zusammenzuhängen =
, ich habe jedoch weder im TeXbook noch in TeX nach Thema eine Erklärung zum „Schützen“ gefunden, aber wahrscheinlich habe ich nicht nach dem richtigen Wort gesucht, um die Erklärung zu finden.
Eine Lösung besteht darin, aus dem bedingten Test "=" auszugeben.
line width=\ifodd \x {1pt} \else 2pt\fi
funktioniert.
Ohne Klammer um 1pt
haben Sie den FehlerMathefehler im Paket PGF: Unbekannte Funktion „pt“ (in „pt“).
Notiz:Besser als die Verwendung einer Klammer um 1pt
, können wir \space
zwischen \ifodd \x
und verwenden 1pt
(danke an Skillmon in den Kommentaren zur Frage).
Bearbeiten:\i
Sie können für den Test und für die Dimension der horizontalen Verschiebung auch direkt verwenden (Sie können also im Befehl unterdrücken [evaluate =\i as \x using int(\i)]
und xshift=\x mm,
durch ersetzen ).xshift=\i mm,
\draw
Korrigierter Code (ich habe auch die Abmessungen yshift
im Zeichenbefehl geändert, sodass die Linien vertikal zentriert sind):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\coordinate (aux1) at ([xshift=-25mm, yshift=-30mm]current page.north east);
\coordinate (aux2) at ([xshift=-25mm, yshift=30mm]current page.south east);
\begin{scope}[black]
\foreach \i in {0, ..., 4}{
\draw[line width=\ifodd \i\space 1pt\else 2pt\fi]([
xshift=\i mm,
yshift=0mm
]aux1)
--
([
xshift=\i mm,
yshift=0mm]
aux2);
}
\end{scope}
\end{tikzpicture}
\end{document}
Ausgabe (links: das gesamte Dokument, rechts: eine Vergrößerung):
Antwort2
Die Optionen können nicht durch Bedingungen umschlossen werden.
Ich bin mir nicht sicher, warum Sie \x
das so definieren, denn\i
Isteine Ganzzahl.
Es ist einfacher: Verwenden Sie \pgfmathifthenelse
.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[
% remember picture,
% overlay,
shorten >= -10pt
]
\coordinate (aux1) at ([xshift=-25mm, yshift=-30mm]current page.north east);
\coordinate (aux2) at ([xshift=-25mm, yshift=30mm]current page.south east);
\coordinate (aux3) at ([xshift=25mm, yshift=-30mm]current page.north west);
\coordinate (aux4) at ([xshift=25mm, yshift=30mm]current page.south west);
\begin{scope}[black]
\foreach \i in {0, ..., 4}{
\pgfmathifthenelse{isodd(\i)}{1}{2}
\draw[line width=\pgfmathresult pt]
([xshift=\i mm,yshift=-65mm]aux1)
--
([xshift=\i mm,yshift=10mm]aux2);
}
\end{scope}
\end{tikzpicture}
\end{document}
Wie Qrrbrbirlbel in den Kommentaren bemerkt hat, können Sie einfacher tun
\draw[line width=isodd(\i) ? 1pt : 2pt]
die \pgfmathifhenelse
Zeile weglassen.
Oder Sie können Folgendes verwenden expl3
:
\ExplSyntaxOn
\NewExpandableDocumentCommand{\isoddTF}{mmm}
{
\int_if_odd:nTF { #1 } { #2 } { #3 }
}
\ExplSyntaxOff
in der Präambel und
\draw[line width=\isoddTF{\i}{1pt}{2pt}]
im Körper des tikzpicture
.