> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/ar/mzyd-mn-almwdhwaat/02-algorithms.md).

# الخوارزميات

## مقدمة

*(لكتابة قوائم شيفرة البرنامج، يُرجى الرجوع إلى* [*صفحة المساعدة هذه*](/latex/ar/altnsyq/11-code-listing.md) *بدلاً من ذلك.)*

لصفّ الخوارزميات أو الشيفرة الكاذبة في LaTeX يمكنك استخدام أحد الخيارات التالية:

* اختر واحدًا فقط من (`algpseudocode` أو `algcompatible` أو `algorithmic`) من الحزم لصفّ متن الخوارزمية، و `algorithm` حزمة لتسمية الخوارزمية.
* ال `algorithm2e` الحزمة.

**لاحظ أنك يجب أن تختار مجموعة واحدة فقط من الحزم المذكورة أعلاه، وأن تستخدم فقط الأوامر والصياغة التي توفرها الحزمة التي تختارها.** لا يمكن تحميل هذه الحزم في الوقت نفسه؛ وإلا فستحصل على الكثير من الأخطاء.

## حزمتا algpseudocode وalgorithm

ال `algpseudocode` توفّر الحزمة `algorithmic` بيئة وبعض الأوامر المفيدة. يمكنك [فتح مثال كامل على Overleaf](https://www.overleaf.com/project/new/template/20763?id=71202437\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=) ، وسندخل في بعض التفاصيل في هذا القسم.

إليك خوارزميتنا الأولى، باستخدام البيئات والأوامر من `algpseudocode` الحزمة:

```latex
\documentclass{article}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithmic}
\State $i \gets 10$
\If{$i\geq 5$}
    \State $i \gets i-1$
\Else
    \If{$i\leq 3$}
        \State $i \gets i+2$
    \EndIf
\EndIf
\end{algorithmic}

\end{document}
```

[افتح هذا المثال القصير لـ algpseudocode على Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=algpseudocode+short+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgpseudocode%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithmic%7D%0A%5CState+%24i+%5Cgets+10%24%0A%5CIf%7B%24i%5Cgeq+5%24%7D+%0A++++%5CState+%24i+%5Cgets+i-1%24%0A%5CElse%0A++++%5CIf%7B%24i%5Cleq+3%24%7D%0A++++++++%5CState+%24i+%5Cgets+i%2B2%24%0A++++%5CEndIf%0A%5CEndIf+%0A%5Cend%7Balgorithmic%7D%0A%0A%5Cend%7Bdocument%7D)

وهذه هي النتيجة الناتجة:

![Algpseudocode-0.png](/files/422492fc78a6e65173b19ac955f926663d0ed6b7)

*لا ينبغي أن تُحمّل `algorithm2e`, `algcompatible`, `algorithmic` الحزم إذا كنت قد حمّلت بالفعل `algpseudocode`.*

لاحظ أن أسماء الأوامر التي توفرها `algpseudocode` تكون عادةً بصيغة الأحرف الكبيرة في بداية الكلمات، مثل `\State`, `\While`, `\EndWhile`.

إذا كنت ترغب في إضافة أرقام الأسطر إلى الخوارزمية، فيمكنك إضافة رقم السطر الأول إلى `algorithmic` البيئة هكذا: `\begin{algorithmic}[1]` فتحصل على هذه النتيجة:

![Algpseudocode-00.png](/files/fc13a2a53e621a7d3f1f7993fc9624afd27e3f87)

المثال الخوارزمي أعلاه ليس له عنوان ولا رقم. إذا كنت بحاجة إلى خوارزمية بعنوان، فستحتاج أيضًا إلى تحميل `algorithm` الحزمة، وإضافة

```latex
\begin{algorithm}
\caption{...}
...
\end{algorithm}
```

حول `algorithmic` البيئة. يمكنك استخدام `\label{...}` بعد `\caption{...}`، بحيث يمكن الإحالة إلى رقم الخوارزمية بالمرجع المتبادل مع `\ref{...}`.

```latex
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{An algorithm with caption}\label{alg:cap}
\begin{algorithmic}
\Require $n \geq 0$
\Ensure $y = x^n$
\State $y \gets 1$
\State $X \gets x$
\State $N \gets n$
\While{$N \neq 0$}
\If{$N$ is even}
    \State $X \gets X \times X$
    \State $N \gets \frac{N}{2}$  \Comment{This is a comment}
\ElsIf{$N$ is odd}
    \State $y \gets y \times X$
    \State $N \gets N - 1$
\EndIf
\EndWhile
\end{algorithmic}
\end{algorithm}

\end{document}
```

[افتح هذا المثال القصير algorithm+algpseudocode على Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=Captioned+algorithm%2Balgpseudocode+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgorithm%7D%0A%5Cusepackage%7Balgpseudocode%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithm%7D%0A%5Ccaption%7BAn+algorithm+with+caption%7D%5Clabel%7Balg%3Acap%7D%0A%5Cbegin%7Balgorithmic%7D%0A%5CRequire+%24n+%5Cgeq+0%24%0A%5CEnsure+%24y+%3D+x%5En%24%0A%5CState+%24y+%5Cgets+1%24%0A%5CState+%24X+%5Cgets+x%24%0A%5CState+%24N+%5Cgets+n%24%0A%5CWhile%7B%24N+%5Cneq+0%24%7D%0A%5CIf%7B%24N%24+is+even%7D%0A++++%5CState+%24X+%5Cgets+X+%5Ctimes+X%24%0A++++%5CState+%24N+%5Cgets+%5Cfrac%7BN%7D%7B2%7D%24++%5CComment%7BThis+is+a+comment%7D%0A%5CElsIf%7B%24N%24+is+odd%7D%0A++++%5CState+%24y+%5Cgets+y+%5Ctimes+X%24%0A++++%5CState+%24N+%5Cgets+N+-+1%24%0A%5CEndIf%0A%5CEndWhile%0A%5Cend%7Balgorithmic%7D%0A%5Cend%7Balgorithm%7D%0A%0A%5Cend%7Bdocument%7D)

![Algpseudocode-1.png](/files/c759ba4f3c334802b841f938a90eefa2c93a4a9a)

ال `algorithm` البيئة هي [float](/latex/ar/alashkal-waljdawl/02-positioning-images-and-tables.md) مثل `table` و `figure`، لذا يمكنك إضافة معدّلات موضع العائم `[hbt!]` بعد `\begin{algorithm}` إذا لزم الأمر. وهذا يعني أيضًا أنه بينما يمكن لبيئة طويلة `algorithmic` بحد ذاتها أن تنكسر عبر الصفحات، فإن `algorithm` البيئة لن تفعل ذلك.

ال `algorithm` توفّر الحزمة أيضًا `\listofalgorithms` أمرًا يعمل مثل `\listoffigures`، ولكن للخوارزميات ذات العنوان، مثل هذا.

![Algpseudocode-2.png](/files/2db9f4d7c4728b9c42c85575f52e4930bc4446ee)

[افتح مثالًا كاملًا على Overleaf](https://www.overleaf.com/project/new/template/20763?id=71202437\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## حزمتا algcompatible/algorithmic وalgorithm

ال `algorithmic` تستخدم الحزمة صياغة مشابهة لـ `algpseudocode`; **لكن** وأسماء أوامرها تُكتب بأحرف كبيرة، مثل `\STATE`, `\WHILE`, `\ENDWHILE`.

ومن ناحية أخرى، `algcompatible` ستتعرف على أسماء الأوامر المكتوبة بأحرف كبيرة وبصيغة بدء الأحرف الكبيرة، لذا `\STATE`, `\WHILE`, `\ENDWHILE`, `\State`, `\While`, `\EndWhile` تُعرَف كلها. وبصرف النظر عن أسماء الأوامر، `algcompatible` و `algorithmic` تستخدم الأوامر نفس صياغة الوسائط كما في `algpseudocode`.

```latex
\documentclass{article}
\usepackage{algcompatible}
% OR \usepackage{algorithmic}
\begin{document}
\begin{algorithmic}
\STATE $i\gets 10$
\IF {$i\geq 5$}
  \STATE $i\gets i-1$
\ELSE
  \IF {$i\leq 3$}
    \STATE $i\gets i+2$
  \ENDIF
\ENDIF
\end{algorithmic}

\end{document}
```

[افتح هذا المثال القصير لـ algcompatible على Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=algcompatible\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgcompatible%7D%0A%25+OR+%5Cusepackage%7Balgorithmic%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithmic%7D%0A%5CSTATE+%24i%5Cgets+10%24%0A%5CIF+%7B%24i%5Cgeq+5%24%7D+%0A++%5CSTATE+%24i%5Cgets+i-1%24%0A%5CELSE%0A++%5CIF+%7B%24i%5Cleq+3%24%7D%0A++++%5CSTATE+%24i%5Cgets+i%2B2%24%0A++%5CENDIF%0A%5CENDIF+%0A%5Cend%7Balgorithmic%7D%0A%0A%5Cend%7Bdocument%7D)

قد تكون بعض القوالب القديمة أو فئات المستند قد حمّلت `algorithmic`، لذا سيتعين عليك اتباع الصياغة وأسماء الأوامر المتوفرة. *لا ينبغي أن تُحمّل `algorithm2e`, `algpseudocode` الحزم إذا كانت `algorithmic` أو `algcompatible` الحزمة محمّلة بالفعل.*

ال `algorithm` يمكن استخدام الحزمة مع `algorithmic`/`algcompatible` لإضافة عناوين مرقمة إلى الخوارزميات.

[افتح مثالًا كاملًا على Overleaf](https://www.overleaf.com/project/new/template/20768?id=71235978\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## حزمة algorithm2e

ال `algorithm2e` تملك الحزمة بنية صياغة مختلفة تمامًا عن `algpseudocode`, `algcompatible` و `algorithmic` الحزم، لذا يجب أن تكون حذرًا بشأن الحزمة التي تريد استخدامها، أو الحزمة التي حمّلها قالبك.

ال `algorithm2e` توفّر الحزمة `algorithm` البيئة:

```latex
\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}
$i\gets 10$\;
\eIf{$i\geq 5$}
{
    $i\gets i-1$\;
}{
    \If{$i\leq 3$}
    {
        $i\gets i+2$\;
    }
}
\end{algorithm}

\end{document}
```

[افتح هذا المثال القصير لـ algorithm2e على Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=algorithm2e+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgorithm2e%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Balgorithm%7D%0A%24i%5Cgets+10%24%5C%3B%0A%5CeIf%7B%24i%5Cgeq+5%24%7D%0A%7B%0A++++%24i%5Cgets+i-1%24%5C%3B%0A%7D%7B%0A++++%5CIf%7B%24i%5Cleq+3%24%7D%0A++++%7B%0A++++++++%24i%5Cgets+i%2B2%24%5C%3B%0A++++%7D%0A%7D%0A%5Cend%7Balgorithm%7D%0A%0A%5Cend%7Bdocument%7D)

![Algorithm2e-0.png](/files/c83dbbe00b67f129454f825436706bb561b528e9)

كل سطر في الشيفرة المصدرية الخاصة بك *يجب* ينتهي بـ `\;` وإلا فستستمر خوارزميتك على السطر نفسه من النص في المخرجات. فقط الأسطر التي تبدأ فيها ماكرو كتلة لا ينبغي أن تنتهي بـ `\;`.

عند استخدام `algorithm2e` يمكنك استخدام `\caption{...}\ref{...}` داخل هذه `algorithm` البيئة مباشرةً، دون الحاجة إلى تحميل أي حزم أخرى. ومع ذلك، إذا أردت إضافة تعليقات إلى خوارزميتك، فستحتاج أولًا إلى التصريح باسم الأمر المراد استخدامه:

```latex
%% This declares a command \Comment
%% The argument will be surrounded by /* ... */
\SetKwComment{Comment}{/* }{ */}

\begin{algorithm}
\caption{An algorithm with caption}\label{alg:two}
\KwData{$n \geq 0$}
\KwResult{$y = x^n$}
$y \gets 1$\;
$X \gets x$\;
$N \gets n$\;
\While{$N \neq 0$}{
  \eIf{$N$ is even}{
    $X \gets X \times X$\;
    $N \gets \frac{N}{2}$ \Comment*[r]{This is a comment}
  }{\If{$N$ is odd}{
      $y \gets y \times X$\;
      $N \gets N - 1$\;
    }
  }
}
\end{algorithm}
```

[افتح هذا المثال المرقم لـ algorithm2e على Overleaf](https://www.overleaf.com/docs?engine=\&snip_name=Captioned+algorithm2e+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Balgorithm2e%7D%0A%5Cbegin%7Bdocument%7D%0A%25%25+This+declares+a+command+%5CComment%0A%25%25+The+argument+will+be+surrounded+by+%2F%2A+...+%2A%2F%0A%5CSetKwComment%7BComment%7D%7B%2F%2A+%7D%7B+%2A%2F%7D%0A%0A%5Cbegin%7Balgorithm%7D%0A%5Ccaption%7BAn+algorithm+with+caption%7D%5Clabel%7Balg%3Atwo%7D%0A%5CKwData%7B%24n+%5Cgeq+0%24%7D%0A%5CKwResult%7B%24y+%3D+x%5En%24%7D%0A%24y+%5Cgets+1%24%5C%3B%0A%24X+%5Cgets+x%24%5C%3B%0A%24N+%5Cgets+n%24%5C%3B%0A%5CWhile%7B%24N+%5Cneq+0%24%7D%7B%0A++%5CeIf%7B%24N%24+is+even%7D%7B%0A++++%24X+%5Cgets+X+%5Ctimes+X%24%5C%3B%0A++++%24N+%5Cgets+%5Cfrac%7BN%7D%7B2%7D%24+%5CComment%2A%5Br%5D%7BThis+is+a+comment%7D%0A++%7D%7B%5CIf%7B%24N%24+is+odd%7D%7B%0A++++++%24y+%5Cgets+y+%5Ctimes+X%24%5C%3B%0A++++++%24N+%5Cgets+N+-+1%24%5C%3B%0A++++%7D%0A++%7D%0A%7D%0A%5Cend%7Balgorithm%7D%0A%0A%5Cend%7Bdocument%7D)

![Algorithm2e-1.png](/files/b1a60f4dd702f4f8c11a0b0c3372b738e22c42e3)

بشكل افتراضي، `plain` يُستخدم نمط algorithm. ولكن إذا كنت تفضّل وجود خطوط حول الخوارزمية والعنوان، فيمكنك إضافة `ruled` خيار الحزمة عند تحميل `algorithm2e`، أو اكتب `\RestyleAlgo{ruled}` في مستندك. وستُصفّ خوارزمياتك ذات العنوان بعد ذلك هكذا:

![Algorithm2e-11.png](/files/d89e5c0fb77ace7e0cb1007da6bbc2b7657ae7df)

ال `algorithm2e` توفّر الحزمة العديد من خيارات التخصيص. على سبيل المثال، إذا كنت تريد إزالة الخطوط العمودية التي تحدد كتل while—end while وif—end if، فيمكنك إضافة `noline` خيار الحزمة عند تحميل `algorithm2e`، أو اكتب `\SetNoline`. يوجد أيضًا `\listofalgorithms` أمر متاح أيضًا في `algorithm2e`.

[افتح مثالًا كاملًا على Overleaf](https://www.overleaf.com/project/new/template/20787?id=71277562\&templateName=algpseudocode+%2B+algorithm+example\&latexEngine=pdflatex\&texImage=texlive-full%3A2020.1\&mainFile=)

## قراءات إضافية

* [`algpseudocode` توثيق الحزمة](https://texdoc.org/pkg/algorithmicx) (انظر القسم 3.1)
* [`algorithmic` و `الخوارزم` توثيق الحزمة](https://texdoc.org/pkg/algorithms)
* [`algorithm2e` توثيق الحزمة](https://texdoc.org/pkg/algorithm2e)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/ar/mzyd-mn-almwdhwaat/02-algorithms.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
